所以我正在尝试通过设计使用 Google oauth 配置 Rails,我遵循了此处描述的官方文档。在点击谷歌注册按钮时,我得到了这个错误
Access to fetch at 'url' (redirected from 'http://localhost:3000/users/auth/google_oauth2') 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.
谷歌搜索我发现你需要启用 CORS,我自然就是这样做的
在我application.rb
的中添加了这个
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: [:get, :post, :patch, :put]
end
end
这是我的回调控制器
class Users::CallbacksController < Devise::OmniauthCallbacksController
skip_before_action :verify_authenticity_token
def google_oauth2
admin = User.from_omniauth(from_google_params)
redirect_to root_path
end
private
def from_google_params
@from_google_params ||= {
uid: auth.uid,
email: auth.info.email,
full_name: auth.info.name,
avatar_url: auth.info.image,
is_member: false
}
end
def auth
@auth ||= request.env['omniauth.auth']
end
end
编辑
所以经过大量的试验 n 错误后,我让它工作了,有点。所以当我点击谷歌注册时,它仍然会抛出一个错误,
但是,当点击谷歌控制台中的链接时,它成功地去那里,有什么想法吗?