Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passport-Google-OAuth Callback Not working

I have the following Node code using passport-google-oauth...

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

app.get('/auth/google/callback', function(req,res) {
    console.log("callback");
    passport.authenticate('google', {
                successRedirect : '/signin',
                failureRedirect : '/signin'
    });
});

and...

passport.serializeUser(function(user, done) {
    console.log("ser");
    done(null, user.id);
});

passport.deserializeUser(function(id, done) {
    console.log("des");
    User.findById(id, function(err, user) {
        done(err, user);
    });
});

passport.use(new GoogleStrategy({

    clientID        : 'id',
    clientSecret    : 'key',
    callbackURL     : 'http://host/auth/google/callback',
},
function(token, rtoken, profile, done) {
   console.log("proc");
   console.log(profile);
   done(null, profile);
}));

The problem is, the callback is getting called but nothing else happens. The processing function never hits. The callback ends up timing out. Any ideas where I went wrong?

like image 417
user1502301 Avatar asked Oct 06 '14 15:10

user1502301


2 Answers

i just found out that passport-google-oauth package exports the following:

exports.Strategy =
exports.OAuthStrategy = OAuthStrategy;
exports.OAuth2Strategy = OAuth2Strategy;

which means, that the "default" (ie. Strategy) is not oauth2 at all... So you better use OAuth2Strategy explicitly. it worked for me. Took me hours to find out this was the problem...

like image 145
Avi Tshuva Avatar answered Sep 22 '22 08:09

Avi Tshuva


You didn't use "passport.authenticate('google')" middleware inside the second route '/auth/google/callback'.

your second route should be like :

app.get( '/auth/google/callback', 
    	passport.authenticate( 'google', { 
    		successRedirect: '/',
    		failureRedirect: '/login'
}));
like image 36
Omkar Jadhav Avatar answered Sep 21 '22 08:09

Omkar Jadhav