2
votes

I am trying to use google authentication in my react, express App. I set up two callbacks redirect URI on my google auth20 client ID as shown in the attachment. I configured react proxy to redirect to the server whenever a request is made to /auth/google or /api/* in setupProxy.js because I use creat react app v2. The app redirect fine from the client-side and google authenticate the login user but google keep redirecting to http://localhost:5000/auth/google/callback after login in the user. I want to redirect to http://localhost:3000/auth/google/callback i.e. my client uri after the user is successfully login. for example localhost:3000/surveys instead of localhost:5000/surveys

My setupProxy.js file content

 const { createProxyMiddleware } = require('http-proxy-middleware')
    module.exports = function (app) {
      app.use(
        ['/auth/google', '/api/*'],
        createProxyMiddleware({
          target: 'http://localhost:5000',
          changeOrigin: true
        })
      )
    }

My Passport and express route handler

const passport = require('passport')
module.exports = app => {
      app.get(
        '/auth/google',
        passport.authenticate('google', {
          scope: ['profile', 'email']
        })
      )
      app.get(
        '/auth/google/callback',
        passport.authenticate('google'),
        (req, res) => {
          res.redirect('/surveys')
        }
      )

      app.get('/api/logout', (req, res) => {
        req.logout()
        res.redirect('/')
      })

      app.get('/api/current_user', (req, res) => {
        res.send(req.user)
      })
    }

My google auth20 Client ID Settings enter image description here

Authorized redirect URIs http://localhost:3000/auth/google/callback http://localhost:5000/auth/google/callback

As Attached.

1
Did you resolve it? - michal

1 Answers

0
votes

You just need to remove the line changeOrigin: true in the option object passed in the createProxyMiddleware function.