我在将 res var 传递到护照验证中间件模块时遇到困难。
按照护照指南操作:http://passportjs.org/guide/authenticate/ 它指出要将 res 传递给authenticate 方法以启用自定义重定向,您需要将 passport.authenticate 放入 app.post 回调中。
因为我想将所有业务保留在我的护照文件中,而不是在 route ,所以我想出了以下内容:
路线:
// process the login form
app.post('/login', function( req, res, next ){
console.log(1);
passport.authenticate('local-login', {
successRedirect : '/profile',
failureRedirect : '/login',
failureFlash : true
});
});
模块:
....
// expose this function to our app using module.exports
module.exports = function(passport) {
....
passport.use('local-login', new LocalStrategy({
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true
},
function(req, email, password, done) {
console.log(2)
// we are checking to see if the user trying to login already exists
User.findOne({ 'local.email' : email }, function(err, user) {
// if there are any errors, return the error before anything else
if (err){
return done(err);
}
console.log(3)
// if no user is found, return the message
if (!user)
return done(null, false, req.flash('loginMessage', 'No user found.'));
// if the user is found but the password is wrong
if (!user.validPassword(password))
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.'));
if( !user.local.authenticated )
return res.redirect( '/resend-activation/' + user.local.email );
// all is well, return successful user
return done(null, user);
});
}));
但是从帖子到此路由的日志是:
server-0 (out): 1
server-0 (out): POST /login 200 120002ms
server-0 (out): 2
server-0 (out): POST /login 200 120090ms
就是这样。它永远不会到达 console.log(3);
我不太确定我在这里做错了什么,是不是因为app.post回调中的req
覆盖了passport.auth中的req
?
任何帮助将不胜感激,谢谢。
约翰
最佳答案
好吧,看来我可能刚刚找到了答案..我有时不知道这是怎么发生的..你写了Q帖子,你有一个 Eureka 时刻..
首先,我错过了实际运行 auth 函数的括号,因此我更新了路线:
app.post('/login', function( req, res, next ){
passport.authenticate('local-login', {
successRedirect : '/profile',
failureRedirect : '/login',
failureFlash : true
})(req, res, next);
});
然后在身份验证护照中,我打印了所有正在传递的参数,并注意到 res 在 req 中。因此,执行自定义重定向的工作方式如下:req.res.redirect...
:
passport.use('local-login', new LocalStrategy({
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true },
function(req, email, password, done) {
console.log( arguments.length );
console.log( arguments );
User.findOne({ 'local.email' : email }, function(err, user) {
// if there are any errors, return the error before anything else
if (err)
return done(err);
console.log(123);
// if no user is found, return the message
if (!user)
return done(null, false, req.flash('loginMessage', 'No user found.'));
// if the user is found but the password is wrong
if (!user.validPassword(password))
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.'));
if( !user.local.authenticated )
return req.res.redirect( '/resend-activation/' + user.local.email );
// all is well, return successful user
return done(null, user);
});
}));
我不知道这是否是正确的做事方式..?
关于node.js - Passport.use 本地登录访问资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29304921/