Devise-OmniAuth Facebook 登录卡住了 CORS 问题

问题描述

所以我一直在设置一个相当乏味的框架 Rails 应用程序,但我很难解决一个我似乎无法解决的 CORS 问题。 CORS 结果如下:

Access to fetch at 'https://www.facebook.com/v4.0/dialog/oauth?client_id=<client_id>&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Fauth%2Ffacebook%2Fcallback&response_type=code&scope=email&state=511bb14a4b678404f0fc4fc4b52a641439077a27ecd5dac4' (redirected from 'http://localhost:3000/auth/auth/facebook') from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs,set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

关注:

GET https://www.facebook.com/v4.0/dialog/oauth?client_id=<client_id>&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Fauth%2Ffacebook%2Fcallback&response_type=code&scope=email&state=638479b154f07d92d9561547b01f9a392d35c523fe8e4776 net::ERR_Failed

我只使用 Postgres 启动了 Rails 6,添加了 Devise,然后添加omniauth-facebook gem。为了尝试解决这个问题,我确保我用来提示 Facebook 登录链接post。我已经尝试过 URL 链接和按钮。我不是用 JS 做这个,只是普通的 ERB 模板:

  <%#= button_to "Sign in with Facebook",user_facebook_omniauth_authorize_path %>
  <%= link_to "Sign in with Facebook",user_facebook_omniauth_authorize_path,method: :post %>

然后我一直环顾四周,发现有人说 rack-cors 为其他人解决了问题,但对我却没有。我的配置如下:

Rails.application.config.middleware.insert_before 0,Rack::Cors do
  allow do
    origins '*'
    resource '*',:headers => :any,:methods => [:get,:post,:delete,:put,:options],:expose  => ['access-token','expiry','token-type','uid','client','Access-Control-Allow-Origin'],:max_age => 0
  end
end

Rails.application.config.hosts << ".facebook.com"

最后一行是我为弄清楚我遗漏了什么所做的最后努力。

此时我不知道下一步该往哪里看。我什至没有为虚拟索引页以外的任何东西创建自定义视图,所以我不知道在哪里看。任何为我指明正确方向的帮助表示赞赏。

编辑:我已在此处按原样上传我的项目(删除我的 Facebook 应用程序密钥):https://github.com/firrae/devise-omniauth-facebook-CORS-error-example。我敢肯定这在这一点上是愚蠢的,但我不知道从哪里看这一点。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)