2015-11-26 17 views
1

我對Nodejs/ExpressJS相當陌生,但是我在ROR的web後端開發方面有足夠的經驗。Nodejs:Passport-local vs定製用戶認證

我使用Passport-local在Node/Express中創建了一個小型Web應用程序。自從我使用Mongodb以來,甚至有一個插件https://github.com/saintedlama/passport-local-mongoose。我用這個。

var User = new Schema({ 
    username: String, 
    password: String, 
}); 

我可以註冊/登錄/註銷。所以基本的應用程序正在工作。

現在我的問題是,如果我只有local身份驗證方法,使用護照很好(不需要社交登錄)。 這意味着認證只使用express and mongoose,沒有額外的插件。

如果是的我怎麼能使用這些只有身份驗證,任何來源或提示?

如果我必須使用護照(或者你推薦護照,因爲這支持身份驗證中間件,我不確定這個,但我怎麼能忽略passport-local-mongoose創建用戶模型和認證使用護照和快遞只要。

回答

1

您可以爲您的護照編寫自己的身份驗證處理程序,該程序連接到包含您登錄信息的任何來源。

// And this handler to your local strategy initialization 
var localStrategyHandler = function (email, password, done) { 

    // Here you can write any function which validates the email and password against the storage yo've used 
    Accounts.getAccount(username, password).then(function (account) { 
     if (account !== null) { 
      return done(null, account); // If login attempt was successfull return a user object using the done callback 
     } else { 
      return done(null, false, 'ACCOUNT_NOT_FOUND'); // If login failed return a failure like this example 
     } 
    }).catch(function (error) { 
     return done('LOGIN_ERROR'); 
    }); 
}; 

// Thats how you initilize a strategyHandler 
passport.use(new LocalStrategy(localStrategyHandler)); 

功能done是回調方法來觸發護照進一步行爲。