登录后进行React Express paspport和Google身份验证重定向

时间:2020-03-31 09:49:44

标签: node.js reactjs express authentication passport.js

我试图在我的反应中使用Google身份验证,表示App。如附件所示,我在Google auth20客户端ID上设置了两个回调重定向URI。我配置了react代理,以便在setupProxy.js中向/auth/google/api/*发出请求时重定向到服务器,因为我使用creat react app v2。该应用程序可以从客户端进行重定向,而google可以对登录用户进行身份验证,但是google在用户登录后仍会重定向到http://localhost:5000/auth/google/callback。我想在用户成功登录后重定向到http://localhost:3000/auth/google/callback,即我的客户端uri。例如localhost:3000/surveys代替localhost:5000/surveys

我的setupProxy.js文件内容

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

我的护照和快递路线处理程序

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)
      })
    }

我的google auth20客户端ID设置 enter image description here

授权的重定向URI http://localhost:3000/auth/google/callback http://localhost:5000/auth/google/callback

随附。

1 个答案:

答案 0 :(得分:0)

您只需要删除changeOrigin: true函数中传递的选项对象中的行createProxyMiddleware