【发布时间】:2022-11-01 16:32:34
【问题描述】:
技术:ReactJS、NodeJS、Express、Passeport、Rest API
我正在尝试使用护照进行身份验证(例如在 Github 中)。 这是以下步骤:
- 用户点击“登录到服务”(前端)
- Axios 在我的后端(localhost:3000/auth/Github + header)上获取请求(带有 JWT 令牌)
- 后端将用户重定向到“Github 允许应用程序”以允许我的应用程序。
- Github 重定向到 localhost:3001/success
(3000 是后端,3001 是前端)
但是现在,当用户单击按钮时,github 拒绝并出现此错误:
访问 'https://github.com/login/oauth/authorize?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Fgithub%2Fcallback&scope=user%3Aemail%2Cuser%3Afollow%2Cread%3Auser %2Crepo%2Cpublic_repo&state={XXXXX}&client_id={XXXXX}'(从 'http://localhost:3000/auth/Github' 重定向)已被 CORS 策略阻止:响应预检请求未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。如果不透明的响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用 CORS 的资源。
我已将 id 和 state 替换为 {XXXXX}
在我的后端我有这条线:
app.use(cors());这是我的前台要求:
await http.get("/auth/Github", { headers: { Authorization: token, mode: "no-cors", }, });令牌有效,我有来自本地存储的日志。
【问题讨论】:
标签: reactjs