使用代理目标将用户从 localhost:3000 带到 localhost:3000/auth/google 不起作用

问题描述

用户点击使用 Google 登录时,用户将被带到 Google OAuth 流程以进行登录程序。但是在点击浏览器时,只需将其 url 更改为 localhost:3000/auth/google 并且没有任何反应。 如果我明确提供完整的 href 即

,它工作正常

http://localhost:5000/auth/google

应用组件:

import './App.css';
import React,{ Component } from 'react';
class App extends Component {
  render() {
    return (
      <div className="App">
        <a href="/auth/google">Sign in using google</a>
      </div>
    );
  }
}
export default App;

package.json

{
  "name": "client","version": "0.1.0","private": true,"proxy": {
    "/auth/google": {
      "target": "http://localhost:5000/"
    }
  },

解决方法

在 package.json 上删除您的代理并尝试此操作 在你的 src 目录上创建 setupProxy.js 然后 npm install http-proxy-middleware

const { createProxyMiddleware } = require("http-proxy-middleware");

const proxy = require("http-proxy-middleware");
module.exports = function (app) {
  app.use(
    createProxyMiddleware(
      "/auth/google",// replace with your endpoint
      { target: "http://localhost:5000" } // replace with your target
    )
  );
};