passport icon indicating copy to clipboard operation
passport copied to clipboard

passport-google-oauth20 passport.authenticate doesn't run

Open t845270g opened this issue 2 years ago • 9 comments
trafficstars

When I visit the Google oauth authentication page:

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

My passport about Google oauth authentication strategy:

passport.use(
    new GoogleStrstedy({
    clientID: GOOGLE_CLIENT_ID,
    clientSecret: GOOGLE_CLIENT_SECRET,
    callbackURL: `/auth/google/callback`,
    scope: ['profile', 'email'],
},(accessToken,refreshToken,profile,done)=>{... })  
    
))

redirect page:

app.get("/auth/google/callback",passport.authenticate("google", { session: false, failureRedirect: CLIENT_IP+ "error" }),(req, res) => {
      if (req.user.success) {
                    res.redirect(CLIENT_IP);
            });
      } else {
        res.redirect(CLIENT_IP + "/Register_google?mail=" + req.user.data.mail);
      }
    }
);

When I use Google authentication with localhost:5000 as the backend server, it successfully redirects to the specified page after selecting the authentication account in Google. However, when I upload the frontend of the website to AWS S3 and configure it with CloudFront, and upload the backend to a public EC2 and also configure it with CloudFront, and access the backend server through the HTTPS link generated by CloudFront, the callback function (accessToken, refreshToken, profile, done) => {...} in Google authentication is not executed on the backend server hosted on CloudFront. Instead, the response header redirects to the authentication page: https://accounts.google.com/o/oauth2/v2/auth... Should I adjust the backend code, Google OAuth settings, or AWS configuration to successfully execute the authentication process?

未命名-1-01

t845270g avatar Jul 06 '23 09:07 t845270g

This doesn't seem to be a passport issue. It seems like you are not configuring your oauth callback properly, since you said the code works in your dev environment but not production.

YasharF avatar Jul 21 '23 11:07 YasharF

看來不是護照問題。看來您沒有正確配置 oauth 回調,因為您說代碼可以在您的開發環境中運行,但不能在生產環境中運行。

But I only changed the environment variables used locally to the ones used for the online website, without modifying any other code.

Moreover, the Google OAuth API settings are also directed to the corresponding URL.

t845270g avatar Jul 22 '23 09:07 t845270g

The docs explain it.

YasharF avatar Jul 23 '23 21:07 YasharF

The docs explain it. Could you please share where in the documentation this is explained? I have been trying to figure out the answer to this issue for almost a month, but still haven't found it. Thank you. : )

t845270g avatar Jul 24 '23 12:07 t845270g

https://developers.google.com/identity/protocols/oauth2/web-server

YasharF avatar Jul 24 '23 16:07 YasharF

https://developers.google.com/identity/protocols/oauth2/web-server

I have solved the issue. My Passport Google OAuth 2.0 is not the problem. The issue lies in the deployment on CloudFront, where the cache is not accepting the req.query parameters. This caused an error in the redirected route as it couldn't retrieve the code parameter. Still, thank you very much for your help.

https://developers.google.com/identity/protocols/oauth2/web-server

t845270g avatar Aug 02 '23 18:08 t845270g

where the cache is not accepting the req.query parameters. This caused an error in the redirected route as it couldn't retrieve the code parameter. Still, thank you very much for your help.

I have the same problem with you. How do you solve that ?

tmt203 avatar Jan 18 '24 22:01 tmt203

其中快取不接受 req.query 參數。這導致重定向路由出現錯誤,因為它無法檢索程式碼參數。不過,非常感謝您的幫忙。

我跟你有同樣的問題。你怎麼解決這個問題? https://github.com/jaredhanson/passport/issues/989#issuecomment-1662738087

t845270g avatar Mar 21 '24 06:03 t845270g

can you tell how you figure that issue and resolve exactly i have same problem my google oauth working in development but not in production

17prateek12 avatar Aug 04 '24 05:08 17prateek12