passport-google-oauth2
passport-google-oauth2 copied to clipboard
Cannot get refresh token
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) => { ... }));
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);
};
The combination prompt: 'consent', accessType: 'offline'
worked for me. Same result here: https://github.com/jaredhanson/passport-google-oauth2/issues/27
I ran into this as well and called passport.authorize
instead of passport.authenticate
. This with consent
and accessType: 'offline'
works for me.
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.
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.