Redirecting to previous page after authentication in node.js using passport.js

前端 未结 7 1206
悲&欢浪女
悲&欢浪女 2020-11-30 17:13

I\'m trying to establish a login mechanism using node.js, express and passport.js. The Login itself works quite nice, also sessions are stored nicely with redis but I do hav

7条回答
  •  渐次进展
    2020-11-30 17:58

    In your ensureAuthenticated method save the return url in the session like this:

    ...
    req.session.returnTo = req.originalUrl; 
    res.redirect('/login');
    ...
    

    Then you can update your passport.authenticate route to something like:

    app.get('/auth/google/return', passport.authenticate('google'), function(req, res) {
        res.redirect(req.session.returnTo || '/');
        delete req.session.returnTo;
    }); 
    

提交回复
热议问题