Custom returnUrl on Node.js Passport's Google strategy

前端 未结 4 1103
不知归路
不知归路 2020-12-08 05:21

I\'m using Express and Passport OpenID Google strategy and I would like to set returnURL on each auth request to be able to return to the page that initiated that auth.

4条回答
  •  眼角桃花
    2020-12-08 05:34

    Wherever you have your login button, append the request's current URL as a query parameter (adjust for whatever templating system you use):

    Log In
    

    Then, add middleware to your GET /auth/google handler that stores this value in req.session:

    app.get('/auth/google', function(req, res, next) {
      req.session.redirect = req.query.redirect;
      next();
    }, passport.authenticate('google'));
    

    Finally, in your callback handler, redirect to the URL stored in the session:

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

提交回复
热议问题