Every time I try to invoke the callback URL with google-OAuth2, I encounter the following error:
Error Traceback:
TokenError: Bad Request
at Strategy.OAuth2Strategy.parseErrorResponse (G:\projects\oauth\node_modules\passport-oauth2\lib\strategy.js:358:12)
at Strategy.OAuth2Strategy._createOAuthError (G:\projects\oauth\node_modules\passport-oauth2\lib\strategy.js:405:16)
at G:\projects\oauth\node_modules\passport-oauth2\lib\strategy.js:175:45
at G:\projects\oauth\node_modules\oauth\lib\oauth2.js:191:18
at passBackControl (G:\projects\oauth\node_modules\oauth\lib\oauth2.js:132:9)
at IncomingMessage.<anonymous> (G:\projects\oauth\node_modules\oauth\lib\oauth2.js:157:7)
at IncomingMessage.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
GoogleStrategy:
passport.use(
new GoogleStrategy({
//options for the gooogle strategy
clientID: keys.google.clientID,
clientSecret: keys.google.clientSecret,
callbackURL:"http://localhost:3000/auth/google/redirect",
},(accessToken,refreshToken,profile,done) => {
// Check if user exist in our database
User.findOne({googleId: profile.id}).then((currentUser)=>{
if(currentUser){
// already have the user
console.log('user is ', currentUser);
done(null,currrentUser);
}else{
// if not create a user in db
new User({
username: profile.displayName,
googleId: profile.id
}).save().then((newUser) =>{
console.log('new user created' , newUser);
done(null,newUser);
});
}
})
})
);
Callback Routes:
//auth with google
router.get('/google',passport.authenticate('google',{
scope:['profile']
}));
//callback for google to redirect to
router.get('/google/redirect',passport.authenticate('google'),(req,res) =>{
//res.send(req.user);
res.redirect('/profile');
});
I am completely new to this field. Could someone explain why I am facing this error?