Google oauth does not return email passport authentication

I am trying to log in using the Google button using the js host passport module. I am trying to get a personโ€™s email id, name, profile picture. I am trying to upload rice to a local server. Google does not return the email id even after adding it to the "email" field, and the returned link for the profile does not work. I examined various answers to this question, but everyone is talking to enable userinfo.email. It is out of date now. According to Google documentation, the new scope parameter is email.

Below is my code. Any help is appreciated.

The passport

passport.use(new GoogleStrategy({ clientID : configAuth.googleAuth.clientID, clientSecret : configAuth.googleAuth.clientSecret, callbackURL : configAuth.googleAuth.callbackURL, }, function(token, refreshToken, profile, done) { // make the code asynchronous // User.findOne won't fire until we have all our data back from Google process.nextTick(function() { // try to find the user based on their google id User.findOne({ 'google.id' : profile.id }, function(err, user) { if (err) return done(err); if (user) { // if a user is found, log them in return done(null, user); } else { // if the user isnt in our database, create a new user var newUser = new User(); console.log(profile); //JSON.parse(profile); // set all of the relevant information newUser.google.id = profile.id; newUser.google.token = profile.token; newUser.google.name = profile.displayName; newUser.google.uname = profile.emails[0].value; // pull the first email newUser.google.dp = profile._json.picture; console.log('url is'); console.log(newUser.google.name); console.log(newUser.google.dp); //console.log(profile.picture); Download(newUser.google.uname, newUser.google.dp,function(err){ if(err) console.log('error in dp'); else console.log('Profile Picture downloaded'); }); // save the user newUser.save(function(err) { if (err) throw err; return done(null, newUser); }); } }); }); })); }; 

routes.js

  app.get('/connect/google', passport.authorize('google', { scope : ['profile', 'email'] })); // the callback after google has authorized the user app.get('/connect/google/callback', passport.authorize('google', { successRedirect : '/profile', failureRedirect : '/' })); 

download.js

  module.exports = function(username, uri, callback){ var destination; request(uri).pipe(fs.createWriteStream("./downloads/"+username+".png")) .on('close', function(){ console.log("saving process is done!"); }); 
+12
source share
3 answers

I had the same problem and I wrote this area as follows:

 app.get('/connect/google', passport.authenticate('google', { scope: [ 'https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/userinfo.email' ] })); 

And you will receive an email:

 function(accessToken, refreshToken, profile, done) { console.log(profile.emails[0].value); }); 

Hope this helps you.

+20
source

The above answer definitely works, there is another way to get closer to this.

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

In routes.js with profile add email .

This should solve your problem.

+10
source

According to Google documentation for oauth, the first parameter must be openid, and the second can be an email address or a profile, or both.

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

documentation

0
source

All Articles