我是Nodejs/ExpressJS的新手,但我在ROR的web后端开发方面有足够的经验。
我使用passport-local在Node/Express中创建了一个小型web应用程序。由于我使用的是Mongodb,甚至有一个用于该https://github.com/saintedlama/passport-local-mongoose的插件。我用了这个。
var User = new Schema({
username: String,
password: String,
});我可以注册/登录/注销。因此,一个基本的应用程序是有效的。
现在我的问题是,如果我只有local身份验证方法,使用passport好吗?(不需要社交登录)。这意味着只使用express and mongoose进行身份验证,不需要额外的插件。
如果是,我如何仅使用这些、任何来源或提示进行身份验证?
如果我不得不使用passport (或者你推荐passport,因为它支持身份验证中间件,但我不确定这一点),我怎么能忽略passport-local-mongoose,只使用passport和express创建用户模型和身份验证呢?
发布于 2015-11-26 18:04:36
您可以为passport编写自己的身份验证处理程序,它可以连接到包含您的登录名的任何源。
// 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是触发passports进一步行为的回调方法。
https://stackoverflow.com/questions/33935165
复制相似问题