chris_mac
chris_mac

Reputation: 953

Passport w/node.js Google error

I'm attempting to use passport with an express/node.js app. For some reason I can't authenticate using google. Google gives the following error:

Error: invalid_request
Error in parsing the OpenID auth request.

Code:

    passport.serializeUser(function(user, done) {
  done(null, user.id);
});

passport.deserializeUser(function(user, done) {
  done(null, user);
});

var GoogleStrategy = require('passport-google').Strategy;
var UserModel = require('./models/usermodel');

passport.use(new GoogleStrategy({
    returnURL: 'http://localhost:3000/auth/google/return',
    realm: 'http:/localhost:3000'
  },
  function(identifier, profile, done) {
    profile.email = profile.emails[0].value;
    UserModel.findOneAndUpdate({email:profile.email}, {$set:profile, $inc:{logins:1}}, {upsert:true}, done);
  }
));

Upvotes: 3

Views: 2373

Answers (4)

matanox
matanox

Reputation: 13746

It seems returnURL doesn't play nicely when a port number is included in it. This means you may need to switch to the standard port (80), which on some linux flavours won't work unless you tweaked the OS to allow non-root users access to that port. I'd consider OAuth 2.0 though, as suggested above, before diving in.

Upvotes: 0

im_benton
im_benton

Reputation: 2621

first things first. Make sure your gems are up to date. So add this to your package.json and then run npm install or npm update. I am 99% sure this is it since an old google gem broke mine.

{
  "name": "Boilerplate",
  "version": "0.0.2",
  "private": true,
  "scripts": {
    "start": "node app"
  },
  "dependencies": {
    "express": "3.1.0",
    "ejs": "*",
    "stylus": "*",
    "mongoose": "*",
    "connect-mongo": "*",
    "passport": "*",
    "passport-google": "*",
    "passport-twitter": "*",
    "passport-facebook": "*",
    "passport-local": "*"
  }
}

Then Run:

npm install

Here is what I have for the passport info on my server.js

passport.serializeUser(function(user, done) {
  done(null, user.id)
})

passport.deserializeUser(function(id, done) {
  UserModel.findOne({ _id: id }, function (err, user) {
    done(err, user)
  })
})

var GoogleStrategy = require('passport-google').Strategy;
var UserModel = require('./models/usermodel'); // A schema that is not optional 

passport.use(new GoogleStrategy({
    returnURL: 'http://localhost:3000/auth/google/return',
    realm: 'http://localhost:3000'
  },
  function(identifier, profile, done) {
    console.log(profile);
      UserModel.findOneAndUpdate({email:profile.email}, {$set:profile, $inc:{logins:1}}, {upsert:true}, done);
  }
));

Upvotes: 0

Tom Frost
Tom Frost

Reputation: 1005

I know how late this is, but I thought it was worth noting that your realm is incorrect and that's probably why you got this error. You only have one slash after the protocol -- http:/local instead of https://local.

Upvotes: 0

Jared Hanson
Jared Hanson

Reputation: 16000

If you don't otherwise have reason to use OpenID, I highly recommend switching to OAuth 2.0, which is implemented in passport-google-oauth.

Google seems to be recommending that as their preferred authentication solution, and their OpenID implementation seems to receive less attention and spurious errors occur.

Upvotes: 7

Related Questions