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
Have you checked this documentation from google? It may help.
Also, can you add
.Strategy
at the end ofconst GoogleStrategy = require('passport-google-oauth20');
and see if it helps (or if you get a different error message?