passport-google-oauth2 icon indicating copy to clipboard operation
passport-google-oauth2 copied to clipboard

Cannot get refresh token

Open zzytds opened this issue 6 years ago • 5 comments

Even though I tried many times with several different users, I couldn't get refresh token. My code looks like the following.

passport.use('google', new GoogleStrategy({ clientID: '...', clientSecret: '...', callbackURL: '...', passReqToCallback: true, accessType: 'offline' }, (req, accessToken, refreshToken, profile, cb) => { ... }));

zzytds avatar Apr 29 '18 18:04 zzytds

Same. Seemingly no matter the combination of settings I use, I cannot get a refresh token from this library. The callback is being made and the "oauth dance" is performed. Here is my setup:

Route: auth.get('/google', server.passport.authenticate('google', { prompt: 'consent' }));

Config:

server.passport.use(new GoogleStrategy({
      clientID: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
      callbackURL: server.googleCallback,
      scope: defaultScope,
      accessType: 'offline'
    }, googleVerify)
  );

Callback:

const googleVerify = async (accessToken: string, refreshToken: string, profile: Profile, done: VerifyCallback) => {
    console.log({ accessToken, refreshToken, ...profile});

    const user = await server.repos.oauth.findOrCreate(refreshToken, accessToken, profile._json.email, profile._json.picture);
    return done(null, user);
  };

glinesbdev avatar Jul 11 '19 16:07 glinesbdev

The combination prompt: 'consent', accessType: 'offline' worked for me. Same result here: https://github.com/jaredhanson/passport-google-oauth2/issues/27

schnerd avatar Aug 30 '19 17:08 schnerd

I ran into this as well and called passport.authorize instead of passport.authenticate. This with consent and accessType: 'offline' works for me.

nonken avatar Apr 06 '20 16:04 nonken

Thank you, I've had the same issue and accessType: 'offline', prompt: 'consent', solved it. I've spent an hour scratching my head because I wrote access_type and this saved me at least another hour. Documentation regarding google seems to often have some shortcomings.

josephhk avatar Jul 31 '20 01:07 josephhk

const GoogleStrategy = require('passport-google-oauth20').Strategy

passport.use(new GoogleStrategy({
  clientID: process.env.GOOGLE_CLIENT_ID,
  clientSecret: process.env.GOOGLE_CLIENT_SECRET,
  callbackURL: process.env.GOOGLE_REDIRECT_URI,
  passReqToCallback: true,
  includeGrantedScopes: true,
  accessType: 'offline',
  prompt: 'consent',
  approvalPrompt: 'force'
},
async (req, accessToken, refreshToken, expires_in, profile, done) => {
  try {
    console.log("accessToken accessToken :: ", accessToken)
    console.log("refreshToken refreshToken :: ", refreshToken)
    console.log("profile profile profile :: ", profile)
    console.log("expires_in expires_in expires_in :: ", expires_in)
    
    done(null, profile)
    } catch(error) {
    console.log('Error: ', error)
    done(error)
  }
}))

Always I received the refresh_token as undefined. It would be great if someone help to solve this issue.

Raman99444 avatar Jul 03 '23 09:07 Raman99444