Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite loop occurred while connecting to the passport-google-oauth2 for "Sign in with Google". #91

Open
TENSHKUMAR-KKT-2004 opened this issue Feb 7, 2023 · 2 comments

Comments

@TENSHKUMAR-KKT-2004
Copy link

TENSHKUMAR-KKT-2004 commented Feb 7, 2023

issue :

 when i go to the google oauth to sign-in to my app it will loading infinite time .

solution :

  1.create a separate file for create a passport strategy for google oauth 

example code :

   create a file : oauth.js

            // required packages for oauth
           const passport = require('passport')
           const GoogleStrategy = require('passport-google-oauth2').Strategy

           const User = require('./models/userSchema.js')
           // passport-local config

           // create a user strategy for a mongodb
           passport.use(User.createStrategy())

           // config for google oauth
           passport.use(new GoogleStrategy({
               clientID: "process.env.CLIENT_ID",
               clientSecret: "process.env.CLIENT_SECRET",
               callbackURL: "https://localhost:8888/auth/google/secrets"
             },
             (request, accessToken, refreshToken, profile, done)=> {
                 done(null, profile)
             }
           ))

           // Used to stuff a piece of information into a cookie
           passport.serializeUser((user, done) => {
               done(null, user)
           })

           // Used to decode the received cookie and persist session
           passport.deserializeUser((user, done) => {
               done(null, user)
           })
  1. inside app.js we need to require first.

inside app.js

                     require('./oauth.js')

In case, if you've written the function this way

inside app.js

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

it not work for us because it is a our server side request and response.

so we need to convert the code like this.

 inside app.js 

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

now this get request will trigger to oauth google login/sign-in site without any error.

@TENSHKUMAR-KKT-2004 TENSHKUMAR-KKT-2004 changed the title Infinite loop occur when connecting to the google for sign in with Google. Infinite loop occurred while connecting to the passport-google-oauth2 for "Sign in with Google". Apr 11, 2023
@dvijkalsi
Copy link

We don't really need create a separate oauth.js, what was causing my issue was that I wrote this inside app.js

           app.get('/auth/google', (req, res) => {
             passport.authenticate('google', {
               scope: ['profile', 'email']
             })
           })``

instead when I used this in app.js it started working

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

@GGLVXD
Copy link

GGLVXD commented Sep 9, 2023

cool but i want to kms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants