Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passport.js authenticate not working

I'm trying to set up passport for the first time and going with just a single google sign-in option. I registered with google apis so i have all that setup. the relavant code is below, but when my app makes the '/auth/google/' call it just fails with no response or error message. I've switched up the configuration a bunch of ways to no avail. I've also replaced the passport.authenticate('google') with an anonymous function with a console.log to double check my web service is operating correctly and it is. So I know it is getting to the passport.authenticate('google').

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

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

      // use google strategy
  passport.use(new googleStrategy({
      clientID: config.google.clientID,
      clientSecret: config.google.clientSecret,
      callbackURL: config.google.callbackURL,
      scope: 'https://www.google.com/m8/feeds https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'
  },
  function(accessToken, refreshToken, profile, done) {
    console.log(profile);
  }
));


  app.use(passport.initialize());
  app.use(passport.session());


  app.get('/auth/google', passport.authenticate('google'));
  app.get('/auth/google/callback', passport.authenticate('google', { failureRedirect: '/', scope: 'https://www.google.com/m8/feeds' }), signin);

EDIT: Here is my http request, I'm using angular and this function is tied to a ng-click on a button.

$scope.signIn = function () {
    $http({method: 'GET', url: '/auth/google'}).
        success(function (data, status, headers, config) {
            console.log('success');
        }).
        error(function (data, status, headers, config) {
            console.log(data);
            console.log(status);
            console.log(headers);
            console.log(config);
        });
};

Those logs return nothing

like image 592
Collin Estes Avatar asked Jul 02 '13 12:07

Collin Estes


People also ask

How does Passport js handle authorization?

Authorization is performed by calling passport. authorize() . If authorization is granted, the result provided by the strategy's verify callback will be assigned to req.account . The existing login session and req.

What is Passport authentication in node js?

Passport is Express-compatible authentication middleware for Node. js. Passport's sole purpose is to authenticate requests, which it does through an extensible set of plugins known as strategies.

Is it good to use passport js?

Passport is a popular, modular authentication middleware for Node. js applications. With it, authentication can be easily integrated into any Node- and Express-based app. The Passport library provides more than 500 authentication mechanisms, including OAuth, JWT, and simple username and password based authentication.


1 Answers

You need to call done() inside the middleware for the GoogleStrategy

passport.use(new GoogleStrategy({
       ...
  },
  function(accessToken, refreshToken, profile, done) {
    console.log(profile);

    // Add this
    done(null, profile);//profile contains user information
});

Found this here

like image 58
Mike Fielden Avatar answered Oct 21 '22 04:10

Mike Fielden