passport.use local-login acess to res

我很难将res var传递给passportauthentication的中间件模块。

遵循护照指南: http : //passportjs.org/guide/authenticate/它声明要传递res到authenticate方法来启用自定义redirect,您需要将passport.authenticate放到app.postcallback中。

由于我想保留所有的业务在我的护照文件,而不是在路由我想出了以下内容:

路线:

 // 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); }); })); 

然而,从这个path发布的日志是:

 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.postcallback中的req覆盖了passport.auth中的req

任何帮助将大规模赞赏,谢谢。

约翰

好吧,看来我可能刚刚find了答案..我不知道这是怎么回事有时候..你写的Q后,你有一个尤里卡时刻..

首先,我错过了实际运行auth函数的括号,所以我更新到路由:

 app.post('/login', function( req, res, next ){ passport.authenticate('local-login', { successRedirect : '/profile', failureRedirect : '/login', failureFlash : true })(req, res, next); }); 

然后在validation护照上,我打印了所有传递的参数,并注意到res在req中。 所以执行一个自定义的redirect就像这样: 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); }); })); 

我不知道,如果这是一个正确的做事方式或不..