小编Sam*_*ker的帖子

重定向到githubomniauth路由时如何解决CORS错误?

我正在尝试构建一个简单的应用程序,目前仅使用 GitHub 登录进行身份验证。我使用 Rails v5.2.3 作为后端,使用 React 作为前端。目前,我的根组件中有一个按钮,用于向后端发送 ajax 请求。该请求将触发 my 中的一个操作,SessionsController该操作将重定向到/auth/github路由并开始 GitHub 身份验证周期。

我相信这一步是我遇到错误的地方。

我的浏览器控制台向我显示以下错误消息:

Access to XMLHttpRequest at 'https://github.com/login/oauth/authorize?
client_id=db494fb7eadbc0c6129d&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Fgithub%2Fcallback&resp
onse_type=code&state=79557eda02a2340f9c02b5254f053528314ea750704690ae' (redirected from 
'http://localhost:3000/authenticate/github') from origin 'http://localhost:3000' has been blocked by CORS
 policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Run Code Online (Sandbox Code Playgroud)

以下是我认为相关的一些文件:

Gemfile

Access to XMLHttpRequest at 'https://github.com/login/oauth/authorize?
client_id=db494fb7eadbc0c6129d&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Fgithub%2Fcallback&resp
onse_type=code&state=79557eda02a2340f9c02b5254f053528314ea750704690ae' (redirected from 
'http://localhost:3000/authenticate/github') from origin 'http://localhost:3000' has been blocked by CORS
 policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Run Code Online (Sandbox Code Playgroud)

routes.rb

source …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails cors reactjs axios github-oauth

4
推荐指数
1
解决办法
1942
查看次数

标签 统计

axios ×1

cors ×1

github-oauth ×1

reactjs ×1

ruby-on-rails ×1