I’m encountering an issue with retrieving the user’s email address during Google OAuth 2.0 authentication using Passport.js. Despite configuring the appropriate scopes and ensuring that the email scope is included, I am not able to access the user’s email address in the profile object.
Scopes Verified:
OAuth Consent Screen includes scopes for email and profile.
Token includes the email scope.
Checked User Info Endpoint:
Used the endpoint https://www.googleapis.com/oauth2/v3/userinfo?access_token=YOUR_ACCESS_TOKEN to verify email presence.
Email field is missing in the response.
passport.use(new GoogleStrategy({
clientID: process.env.clientid,
clientSecret: process.env.clientsecret,
callbackURL: 'http://localhost:4000/auth/google/callback',
scope: ['openid', 'profile', 'email'],
}, async (token, tokenSecret, profile, done) => {
try {
logInfo(`Creating an account for user with profile id ${profile.id}`, path.basename(__filename), 'GoogleStrategy');
console.log('User Profile:', profile); // Logging the entire profile object for debugging
let user = await prisma.user.findUnique({ where: { googleId: profile.id } });
if (!user) {
user = await prisma.user.create({
data: {
googleId: profile.id,
name: profile.displayName,
email: profile.emails && profile.emails[0] && profile.emails[0].value // Attempting to access the email field
}
});
}
return done(null, user);
} catch (err) {
console.error('Error:', err); // Improved error logging
return done(err);
}
}));
Even though the OAuth consent screen is configured correctly and the email scope is included in the request, the profile object does not contain the user’s email address. Attempts to retrieve the email via the Google User Info API also do
not return the email field.
Ajay Kumar Gupta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1