Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I Have used Passport-Google-OAuth in Node.js web service project. I am using OAuth2Strategy.

The process i have used is i call the web service method to authenticate user from his Gmail account. Initially i serve the Raw HTMl which i receive from calling the Passport-google-OAuth. Which works fine.

Then i login with valid Gmail accounts. Once the Callback Url is called by google the server goes into infinite loop and calls the callback url again and again after fixed interval of time.

My Passport strategy configuration for Google is like this:
JavaScript
// Use the GoogleStrategy within Passport.
    //   Strategies in Passport require a `verify` function, which accept
    //   credentials (in this case, an accessToken, refreshToken, and Google
    //   profile), and invoke a callback with a user object.
    passport.use(new GoogleStrategy({
            clientID        : "948630708036-2t6mestiv81gtv0s9n6iptoava4o1cpa.apps.googleusercontent.com",
            clientSecret    : "omugRnr7nad2yMmefiZdBaLL",
            callbackURL     : "http://localhost:4000/api/auth/google/callback"
        },
        function(token, refreshToken, profile, done) {
            console.log('Inside global callback.');
            // make the code asynchronous
            // User.findOne won't fire until we have all our data back from Google
            process.nextTick(function() {

                // try to find the user based on their google id
                User.findOne({ 'google.id' : profile.id }, function(err, user) {
                    if (err)
                        return done(err);

                    if (user) {

                        // if a user is found, log them in
                        return done(null, user);
                        
                    } else {
                        // if the user isnt in our database, create a new user
                        var newUser          = new User();

                        // set all of the relevant information
                        newUser.google.id    = profile.id;
                        newUser.google.token = token;
                        newUser.google.name  = profile.displayName;
                        newUser.google.email = profile.emails[0].value; // pull the first email

                        return done(null, newUser);
                       
                    }
                });
            });
        }));


Then i am calling the Passport from the endpoint in the service project:

JavaScript
passport.authenticate('google', { session:false,scope : ['profile', 'email'] });


And the Callback URL contains the following code where i am sending the returned Google account details of the user in JSON format to the client which accessed the web service intially.

JavaScript
function(req, res) {
  console.log('Callback by Google:'+res.body+' || '+ res.headers);
  console.log('Response Object:'+util.inspect(res));
  passport.authenticate('google', {	session : false	}),function(req,res){
					console.log('Callback authenticated.User: +req.user);
                    res.json(req.user);
            }



In the Log i am getting "Callback by Google: undefined || undefined".

I am disabling sessions since this will be the API Server feeding data to various clients.

I dont know what mistake i am doing. Kindly point out any resource or example where the Passport-Google-OAuth(OAuth2Strategy) is used in a API(Web Service) server. Do i need to follow some other way. Thanks for ur help in advance.
Posted
Updated 21-Jul-18 18:12pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900