Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error at Strategy.OAuth2Strategy.parseErrorResponse - NodeJS passport google oauth2.0

I'm trying to embed google authentication in Node.js using passport and google passport-google-oauth20. The problem is that when the google callback route opens up I get:

Error
at Strategy.OAuth2Strategy.parseErrorResponse (E:\Programowanie\NodeJS\Hydronide\node_modules\passport-oauth2\lib\strategy.js:329:12)
at Strategy.OAuth2Strategy._createOAuthError (E:\Programowanie\NodeJS\Hydronide\node_modules\passport-oauth2\lib\strategy.js:376:16)
at E:\Programowanie\NodeJS\Hydronide\node_modules\passport-oauth2\lib\strategy.js:166:45
at E:\Programowanie\NodeJS\Hydronide\node_modules\oauth\lib\oauth2.js:191:18
at passBackControl (E:\Programowanie\NodeJS\Hydronide\node_modules\oauth\lib\oauth2.js:132:9)
at IncomingMessage.<anonymous> (E:\Programowanie\NodeJS\Hydronide\node_modules\oauth\lib\oauth2.js:157:7)
at emitNone (events.js:110:20)
at IncomingMessage.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1059:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)

I (more or less) follow this tutorial. Here is my code: Routes (starting with '/auth')

'use strict'

const passport = require('passport')
const router = require('express').Router()

router.get(
  '/google',
  (req, res, next) => {
    if (req.query.return) {
      req.session.oauth2return = req.query.return
    }
    next()
  },
  passport.authenticate('google', { scope: ['email', 'profile'] })
)

router.get(
  '/google/callback',
  passport.authenticate('google'),
  (req, res) => {
    const redirect = req.session.oauth2return || '/';
    delete req.session.oauth2return;
    res.redirect(redirect);
  }
);

module.exports = router

There is a passport configuration:

'use strict'
const passport = require('passport')
const keys = require('./keys')
const GoogleStrategy = require('passport-google-oauth20').Strategy
const userController = require('../controllers/user-controller')

const passportConfig = {
  clientID: keys.google.clientId,
  clientSecret: keys.google.clientSecret,
  callbackURL: 'auth/google/callback',
  accessType: 'offline'
}

passport.use(new GoogleStrategy(passportConfig,
  (accessToken, refreshToken, profile, done) => {
  console.log(accessToken, refreshToken, profile, done)
  userController.getUserByExternalId('google', profile.id)
  .then(user => {
    if (!user) {
      userController.createUser(profile, 'google')
      .then(user => {
        return done(null, user)
      })
      .catch(err => {
        return done(err)
      })
    }
    return done(null, user)
  })
  .catch(err => {
    return done(err)
  })
}))

passport.serializeUser((user, cb) => {
  cb(null, user)
})
passport.deserializeUser((obj, cb) => {
  cb(null, obj)
})

As you can see I've added console.log in the new GoogleStrategy second parameter function, but it never fires.

//EDIT I noticed that instead of assign require('passport-google-oauth20').Strategy I used require('passport-google-oauth20'). But fixing it doesn't chang anything, still the same error. What I can add to a question is that in my main fail I call

// sets passport config
require('./config/jwt-auth')
require('./config/google-auth')

// initialize passport
app.use(passport.initialize())

So I don't expect anything wrong in there.

like image 985
Konrad Linkowski Avatar asked Jul 07 '18 18:07

Konrad Linkowski


2 Answers

You have to specify the full url in the callbackURL section of the strategy: for example: when if running the code locally on localhost:3000 with code like this:

passport.use(new googleStrategy({
    clientID:keys.clientID,
    clientSecret:keys.clientSecret,
    callbackURL:'auth/google/callback'
},(accessToken,refreshToken, profile,done)=>{
    console.log(accessToken);
    console.log(refreshToken);
    console.log(profile);
}
));

app.get('/auth',passport.authenticate('google',{

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

The above code will surely throw a TokenError: Bad request. You have to pass the complete URl to have a final code like shown below:

passport.use(new googleStrategy({
    clientID:keys.clientID,
    clientSecret:keys.clientSecret,
    callbackURL:'http://localhost:3000/auth/google/callback'
},(accessToken,refreshToken, profile,done)=>{
    console.log(accessToken);
    console.log(refreshToken);
    console.log(profile);
}
));

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

app.get('/auth/google/callback', 
  passport.authenticate('google'));
like image 77
Stephen Gatana Avatar answered Nov 02 '22 20:11

Stephen Gatana


You can get help by putting some console.log inside your Oauth and Strategy under node modules, Specifically around the line on which you are getting error in logs.

E:\Programowanie\NodeJS\Hydronide\node_modules\passport-oauth2\lib\strategy.js
E:\Programowanie\NodeJS\Hydronide\node_modules\oauth\lib\oauth2.js

This will help you to get the root cause of parsing error . Seems like there is some problem with request/response data.

like image 36
eduPeeth Avatar answered Nov 02 '22 20:11

eduPeeth