我已经为我的应用程序Twitter身份验证弄清楚了这一点,我确信GoogleStrategy非常相似。尝试这样的变体:
假设您已经定义了来自身份验证服务的回调路由(从护照指南中),如下所示:
app.get('/auth/twitter/callback', passport.authenticate('twitter', { successRedirect: authenticationRedirect(req, '/account') , failureRedirect: '/' }));只需将该块更改为:
app.get('/auth/twitter/callback', function(req, res, next){ passport.authenticate('twitter', function(err, user, info){ // This is the default destination upon successful login. var redirectUrl = '/account'; if (err) { return next(err); } if (!user) { return res.redirect('/'); } // If we have previously stored a redirectUrl, use that, // otherwise, use the default. if (req.session.redirectUrl) { redirectUrl = req.session.redirectUrl; req.session.redirectUrl = null; } req.logIn(user, function(err){ if (err) { return next(err); } }); res.redirect(redirectUrl); })(req, res, next);});现在,为经过身份验证的路由定义中间件,以将原始URL存储在会话中,如下所示:
ensureAuthenticated = function (req, res, next) { if (req.isAuthenticated()) { return next(); } // If the user is not authenticated, then we will start the authentication // process. Before we do, let's store this originally requested URL in the // session so we know where to return the user later. req.session.redirectUrl = req.url; // Resume normal authentication... logger.info('User is not authenticated.'); req.flash("warn", "You must be logged-in to do that."); res.redirect('/');}作品!



