JR Galia
JR Galia

Reputation: 17269

PassportJS Profile Undefined Name

I'm using PassportJS and passport-google-oauth in an ExpressJS web app.

var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
passport.use(new GoogleStrategy({
  clientID: GOOGLE_CLIENT_ID,
  clientSecret: GOOGLE_CLIENT_SECRET,
  callbackURL: CALLBACK
},
function(accessToken, refreshToken, profile, done) {
  console.log(profile.displayName);
  console.log(profile.name.familyName);
  console.log(profile.name.givenName);
  ...
}));

The problem is that profile.displayName, profile.name.familyName and profile.name.givenName are undefined. When I use the callback with Passport Facebook, no problem at all.

How to get the name of the user when using a Google account to login?

Upvotes: 0

Views: 1525

Answers (2)

Adonis Lee Villamor
Adonis Lee Villamor

Reputation: 166

When I checked it seems it has more parameters than what is in the official sample leading people to confusion including me..

rather than

function(accessToken, refreshToken, profile, done)

use

function(req, accessToken, refreshToken, profile, done)

Upvotes: 6

boh
boh

Reputation: 1547

you need to request for it, include 'https://www.googleapis.com/auth/userinfo.profile' in your scope.

passport.use(new GoogleStrategy({
  clientID: GOOGLE_CLIENT_ID,
  clientSecret: GOOGLE_CLIENT_SECRET,
  callbackURL: CALLBACK,
  scope: ['https://www.googleapis.com/auth/userinfo.profile','email', ...]
}

Upvotes: 1

Related Questions