Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

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

Open
iamgkstack opened this issue Jan 27, 2020 · 1 comment
Open

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

iamgkstack opened this issue Jan 27, 2020 · 1 comment

Comments

@iamgkstack
Copy link

@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);

Originally posted by @damianobarbati in #5 (comment)

@iamgkstack
Copy link
Author

@natanbr I am trying to implement like below using OAuth2Strategy but I am getting refresh token to undefine If anyone knows please help me.

const computeConnectedUser = strategy => (req, accessToken, refreshToken, profile, done) => {

    console.log('accessToken =>', accessToken);
    console.log('refreshToken =>', refreshToken);

    return done(false, { strategy, accessToken, refreshToken, ...profile });
}

const oAuth2Strategy = new OAuth2Strategy({
    authorizationURL: 'https://accounts.google.com/o/oauth2/v2/auth',
    tokenURL: 'https://www.googleapis.com/oauth2/v4/token',
    clientID: 'xxxxxx',
    clientSecret: 'xxxxxx',
    callbackURL: "http://localhost:3000/auth/google/callback",
    passReqToCallback: true
}, computeConnectedUser('oauth2'));

passport.use(oAuth2Strategy);

app.get('/auth/google', passport.authenticate('oauth2', {
    authType: 'rerequest',
    accessType: 'offline',
    prompt: 'consent',
    session: false,
    includeGrantedScopes: true,
    scope: ['https://www.googleapis.com/auth/analytics.readonly',
    'https://www.googleapis.com/auth/analytics'
    ]
}));
 
app.get('/auth/google/callback',
  passport.authenticate('oauth2', { session: false, failureRedirect: '/auth/google' }),
  function(req, res) {
    // Successful authentication, redirect home.
    console.log('res.body=>', res.accessToken);
    return res.status(200).json({ token: res.accessToken });
});

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant