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

refreshToken

Open sdemoor opened this issue 7 years ago • 10 comments

Hi! I am not able to access the refreshToken inside the 'new Strategy' callback function. When logging in it does ask for offline access, and everything works, except for accessing the refreshtoken.

sdemoor avatar Apr 16 '17 00:04 sdemoor

I can't as well, did you solve this @sdemoor ?

damianobarbati avatar Oct 11 '17 14:10 damianobarbati

Did someone solved it? @damianobarbati @sdemoor @mstade

natanbr avatar Nov 14 '17 02:11 natanbr

@natanbr yes, here what I'm currently doing:

const passportGoogle = require('passport-google-oauth').OAuth2Strategy;

const computeConnectedUser = strategy => (req, accessToken, refreshToken, profile, done) => done(false, { strategy, accessToken, refreshToken, ...profile });

const googleStrategy = new passportGoogle({
    clientID: googleConfig.clientID,
    clientSecret: googleConfig.clientSecret,
    callbackURL: googleConfig.callbackURL,
    profileFields: googleConfig.profileFields,
    passReqToCallback: true,
}, computeConnectedUser('google'));

passport.use(googleStrategy);

router.all(googleConfig.connectURL, passport.authenticate('google', { authType: 'rerequest', accessType: 'offline', prompt: 'consent', includeGrantedScopes: true, scope: googleConfig.scope }));

router.all(googleConfig.callbackURL, passport.authenticate('google', { failureRedirect: googleConfig.connectURL, session: false }), oauthed);

damianobarbati avatar Nov 14 '17 09:11 damianobarbati

Thanks a lot! @damianobarbati The reason it didn't work for me is that I set the params in the strategy I had to set them for the connection URL

This is the working code:

router.get('/:provider', // ConnectionURL
  (req, res, next)=> {
    passport.authenticate(`${req.params.provider}`, { authType: 'rerequest', accessType: 'offline', prompt: 'consent', includeGrantedScopes: true })(req, res, next);
  }
);

natanbr avatar Nov 15 '17 00:11 natanbr

@damianobarbati I'm not sure exactly what you're doing which is making it work for you...

My own implementation is pretty minimal and still my refreshToken is undefined :/

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

    passport.use(new GoogleStrategy({
            clientID: "whatever",
            clientSecret: "whatever",
            callbackURL: "http://localhost:3000/api/auth/google/callback",
            passReqToCallback: true
        },
        (request, accessToken, refreshToken, profile, done) => {
            process.nextTick(() => {
                console.log(refreshToken); //<-------undefined :(
                done(null, null, refreshToken);
            });
        }
    ));

//...the auth route:

    app.get("/api/auth/google/:id", (req, res, next) => {
        passport.authenticate("google", {
            session: false,
            state: req.params.id,
            scope: ["https://www.googleapis.com/auth/plus.login"]
        })(req, res, next);
    });

//...the callback

    app.get("/api/auth/google/callback", (req, res, next) => {
        passport.authenticate("google", (err, user, refreshToken) => {
            console.log(refreshToken);            //<------ still undefined 
        })(req, res, next);
    });

I'm not sure what I'm doing wrong, this is almost exactly how it is in the readme (only difference being that I'm handling redirection myself). I get the accessToken just fine but the refresh token is nowhere to be seen.

@natanbr What do you mean by "I set the params in the strategy"? @damianobarbati What exactly did you change in your code which made it work? @sdemoor Did you get it to work?

Aditya94A avatar Nov 21 '17 15:11 Aditya94A

@AdityaAnand1 double check the following => accessType: 'offline', prompt: 'consent'

damianobarbati avatar Nov 21 '17 15:11 damianobarbati

@damianobarbati Ahh, that did it! I didn't realize that was the extra options bits. Thank you! (I didn't see anything like this in the docs though 😕)

Here's my working connect route for future lost souls:

    app.get("/api/auth/google/:id", (req, res, next) => {
        passport.authenticate("google", {
            accessType: 'offline',
            prompt: 'consent',
            session: false,
            state: req.params.id,
            scope:
                ["https://www.googleapis.com/auth/plus.stream.write",]
        })(req, res, next);
    });

Aditya94A avatar Nov 21 '17 15:11 Aditya94A

(I didn't see anything like this in the docs though 😕)

Yeah the docs really could use some work.

mstade avatar Mar 06 '19 23:03 mstade

I checked and it works without prompt: 'consent'

metacritical avatar Oct 18 '19 18:10 metacritical

Hi Friends, How Can I get tokenId or Access token from response

nayefmhmd85 avatar Dec 09 '20 15:12 nayefmhmd85