admin管理员组

文章数量:1386700

So I'm working off the information that was given here to add the ability that Google will redirect to the page a user was at before it redirected to google. I'm currently using the latest versions of Express, PassportJS, and Google oauth2.

For example, if a user hits page , it'll automaticially redirect to Google asking to sign in, and after it's sucessful it returns to my Node App, except it doesn't know the last page was /privatecontent and instead redirects to the index.

If I understand right, I can use the state parameter to let Google know to send the state param back so I can read it and redirect myself.

I essentially would like my function to look a little something like this, but I don't have access to req.headers, or just don't know how honestly within passport.authenticate.

app.get("/auth/google", passport.authenticate("google", {
  scope: [".profile", ".email"],
  state: base64url(JSON.stringify({
    lastUrl: req.headers['referer']
  }))
}), function(req, res) {});

So I'm working off the information that was given here to add the ability that Google will redirect to the page a user was at before it redirected to google. I'm currently using the latest versions of Express, PassportJS, and Google oauth2.

For example, if a user hits page http://example./privatecontent, it'll automaticially redirect to Google asking to sign in, and after it's sucessful it returns to my Node App, except it doesn't know the last page was /privatecontent and instead redirects to the index.

If I understand right, I can use the state parameter to let Google know to send the state param back so I can read it and redirect myself.

I essentially would like my function to look a little something like this, but I don't have access to req.headers, or just don't know how honestly within passport.authenticate.

app.get("/auth/google", passport.authenticate("google", {
  scope: ["https://www.googleapis./auth/userinfo.profile", "https://www.googleapis./auth/userinfo.email"],
  state: base64url(JSON.stringify({
    lastUrl: req.headers['referer']
  }))
}), function(req, res) {});
Share Improve this question edited Jan 6, 2015 at 14:51 Martijn Pieters 1.1m321 gold badges4.2k silver badges3.4k bronze badges asked Dec 5, 2014 at 3:30 DustinDustin 6,29719 gold badges64 silver badges93 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

Make a custom middleware

function myCustomGoogleAuthenticator(req, res, next){
    passport.authenticate({
        scope: ...
        state: // now you have `req`
    })(req, res, next);
    //^ call the middleware returned by passport.authenticate
}

Add that to your route instead

app.get("/auth/google", myCustomGoogleAuthenticator, function(req, res) {});

本文标签: javascriptPassportJSDynamically set state to allow redirect on callbackStack Overflow