2

I am having trouble getting a refresh token from Google using Passport JS. I have been removing access in my account permissions to make sure it is a first time login. I keep getting "undefined" for the token. Here is the code I have at the moment.

Passport.use(new GoogleStrategy({
    clientID:     "xxxxx.apps.googleusercontent.com",
    clientSecret: "xxxxx",
    callbackURL: `https://localhost:${port}/auth/google/callback`,
    accessType: 'offline',
    passReqToCallback: true
  },
  function(req,accessToken, refreshToken, profile, done) {

    var tokens = {
      access_token: accessToken,
      refresh_token: refreshToken
    }
    console.log(tokens)
    oauth2Client.setCredentials(tokens);


    done(err = false, user = true)


}));

If anyone has experience working OAuth into their web apps that could lend a hand it would be greatly appreciated

2 Answers 2

3

The answer was in requesting it during the innitail route and creating the consent prompt each time.

app.get('/auth/google', passport.authenticate('google',{accessType: 'offline', prompt: 'consent', scope: ['openid profile email https://www.googleapis.com/auth/drive']}))
Sign up to request clarification or add additional context in comments.

Comments

0

To get the refresh token using passportJS in NodeJS you simply have to add 2 more access fields ( accessType: 'offline', prompt: 'consent' ) in the router section.

ex.router.get('/auth/google', passport.authenticate('google', {
    scope: ['profile'],
    accessType: 'offline',
    prompt: 'consent'       
}));

After that you will receive a refresh token.

Note: If your application is not verified by the Google then it will so you warning. Simply click on the advance button below and you are ready to go.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.