jaredhanson / passport

Simple, unobtrusive authentication for Node.js.
https://www.passportjs.org?utm_source=github&utm_medium=referral&utm_campaign=passport&utm_content=about
MIT License
22.95k stars 1.24k forks source link

Error after trying to authenticate with OAuth2.0 strategy #620

Open ghost opened 7 years ago

ghost commented 7 years ago

I am trying to use this strategy (passport-google-oauth20) to get OAuth 2.0 with Google working on my website.

However, after I am redirected back to my own site by Google my site crashes with this error message:

Error
   at /Users/theonlygusti/Project/node_modules/passport-google-oauth20/lib/strategy.js:95:21
   at passBackControl (/Users/theonlygusti/Project/node_modules/oauth/lib/oauth2.js:132:9)
   at IncomingMessage.<anonymous> (/Users/theonlygusti/Project/node_modules/oauth/lib/oauth2.js:157:7)
   at emitNone (events.js:91:20)
   at IncomingMessage.emit (events.js:185:7)
   at endReadableNT (_stream_readable.js:974:12)
   at _combinedTickCallback (internal/process/next_tick.js:74:11)
   at process._tickCallback (internal/process/next_tick.js:98:9)

This error actually appears both on the web page and in the console.

Here's a summary of the code I'm attempting to get to work:

const express=require('express');
const app = express();
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;

passport.use(new GoogleStrategy({
  clientID: process.env.GOOGLE_OAUTH_CLIENT_ID,
  clientSecret: process.env.GOOGLE_OAUTH_CLIENT_SECRET,
  callbackURL: '/sign-in/after'
},
function(accessToken, refreshToken, profile, cb) {
  User.findOrCreate({ googleId: profile.id }, function (err, user) {
    return cb(err, user);
  });
}));

app.get('/sign-in/go', passport.authenticate('google', { scope: ['profile'] }));

// reached
app.get('/sign-in/after',
  passport.authenticate('google',
  // never reached
  { failureRedirect: '/sign-in' }),
  function(req, res) {
    res.redirect('/');
  });

const httpServer = app.listen(process.env.PORT);

How can I stop this error and get the sign in by redirection to work? (My app already works with the JavaScript button sign-in.)

xjamundx commented 6 years ago

Not sure if this helps, but if you look at line 95 the error actually appears to be a Google Plus API error: https://github.com/jaredhanson/passport-google-oauth2/blob/master/lib/strategy.js#L95

Sorry I don't know more than that.

harzkr commented 6 years ago

Hi @theonlygusti

I don't know if your problem persists, I was coming across the same error, as pointed out in the comment above, it's a Google+ API error, it's probably not enabled on the console. You should try it, or any one else who comes across the same

Thanks

naveen1994rai commented 3 years ago

Can you try adding this option, when asking passport to use Google as a strategy ?

passport.use(new GoogleStrategy({ clientID: process.env.GOOGLE_OAUTH_CLIENT_ID, clientSecret: process.env.GOOGLE_OAUTH_CLIENT_SECRET, callbackURL: '/sign-in/after', userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo" }, function(accessToken, refreshToken, profile, cb) { User.findOrCreate({ googleId: profile.id }, function (err, user) { return cb(err, user); }); }));

Basically, you are asking google to use the userinfo api to authenticate the user rather than Google Plus's api.

spalduing commented 1 year ago

@naveen1994rai

Thank you so much, you help me a lot with that suggestion!

<< userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo" >>