Failed to fetch user profile login via google with node and passport

2.3k Views Asked by At

I am trying to login via google using node and package passport-google-oauth20. But getting the error - "InternalOAuthError: Failed to fetch user profile". Already enable google plus api. I am using my business gmail id to access this. Everything is stored in mongodb but not getting the return json as a response. Here is my passport-setup.js code.

const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20');
const keys = require('./keys');
const User = require('../api/models/user');
const mongoose   = require('mongoose');

passport.use(
new GoogleStrategy({
callbackURL: '/users/google/redirect',
clientID: keys.google.clientID,
clientSecret: keys.google.clientSecret
}, (accessToken, refreshToken, profile, done)=>
{

  User.findOne({googleid : profile.id})
  .exec()
  .then(doc=>{
      if(doc)
      {
        //already exist user
        console.log("User exist : "+doc)
      } else{
        const user = new User({
          _id : new mongoose.Types.ObjectId(),
          name : profile.displayName,
          googleid : profile.id
          });
          user.
          save().
          then(result=>{
              console.log("myerror1" +result);
          })
      }
  })
  .catch(err=>{
      console.log("error1" + err);
  });
  console.log(accessToken);
  console.log(profile);
  })
 )

And here is routes code

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

router.get('/google/redirect', passport.authenticate('google'), (req, res)=> 
{
res.send(req.user);
});

I am gettting error on google.redirect request.

req.user

showing me InternalOAuthError

2

There are 2 best solutions below

0
On

Have you checked this documentation from google? It may help.

Also, can you add .Strategy at the end of const GoogleStrategy = require('passport-google-oauth20'); and see if it helps (or if you get a different error message?

const GoogleStrategy = require('passport-google-oauth20').Strategy;
0
On

I faced the same problem and I solved it doing this:

In package.json, change "passport" value to "^0.4.0" and "passport-google-oauth" to "^2.0.0". Run "npm install" again.