passport-google-oauth2
passport-google-oauth2 copied to clipboard
@natanbr yes, here what I'm currently doing:
@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 https://github.com/mstade/passport-google-oauth2/issues/5#issuecomment-344197477
@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 });
});