Passport Google oauth2 does not return email

673 Views Asked by At

I'm trying to access user details using passport google oauth2, it returns id, gender, displayName, name, provider but not email.

passport.use(new GoogleStrategy({
  clientID: '1050835058852-cmniek05f0tdotj4ikp54s8qvvgbd9j4.apps.googleusercontent.com',
  clientSecret: 'LjnSZOwA95bL6hvKMYjWPc_Q',
  callbackURL: 'http://localhost:3000/auth/google/callback',
  profileFields: ['id', 'email', 'gender', 'displayName', 'name', 'provider']
},
function(accessToken, refreshToken, profile, cb) {
    if (profile) {
        user = profile;
        console.log(profile);
        passport.serializeUser(function(user, done) {
            done(null, user);
        });
        passport.deserializeUser(function(user, done) {
            done(null, user);
        });
        return cb(null, user);
    }
    else {
        return cb(null, false);
    }
  }
));

In the profile fields I have specified email, still it sends everything except email.

Here is the get request which I'm trying to get authenticated.

app.get('/auth/google',
    passport.authenticate('google', { scope: ['profile'] })
);

app.get('/auth/google/callback',
  passport.authenticate('google', { failureRedirect: '/auth/google' }),
  function(req, res) {
  // Successful authentication, redirect home.
  res.redirect('/account');
});
1

There are 1 best solutions below

0
On

Add email to the scope. As following:

scope: ['profile', 'email']

If it doesn't render email in the response change the scopes like this:

scope: [
  "https://www.googleapis.com/auth/userinfo.profile",
  "https://www.googleapis.com/auth/userinfo.email"
]