2015-11-26 20 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是回调方法来触发护照进一步行为。