Jér*_*Boé 6 oauth devise ruby-on-rails-4
我有一个模型候选人,设计omniauthable(linkedin).
到目前为止,我的routes.rb看起来像这样:
namespace :v1 do
devise_for :candidates, only: :omniauth_callbacks
...
end
Run Code Online (Sandbox Code Playgroud)
一切顺利,直到我不得不添加新版本:
namespace :v2 do
devise_for :candidates, only: :omniauth_callbacks
...
end
namespace :v1 do
devise_for :candidates, only: :omniauth_callbacks
...
end
Run Code Online (Sandbox Code Playgroud)
使用当前配置,我收到此错误:
`set_omniauth_path_prefix!': Wrong OmniAuth configuration. If you are getting this exception, it means that either: (RuntimeError)
1) You are manually setting OmniAuth.config.path_prefix and it doesn't match the Devise one
2) You are setting :omniauthable in more than one model
3) You changed your Devise routes/OmniAuth setting and haven't restarted your server
Run Code Online (Sandbox Code Playgroud)
这有点烦人,因为我希望能够在两个版本上验证候选者.
我能做什么 ?
jls*_*str 14
好吧,让我们回顾一下一点点在这里,制定不允许调用 devise_for里面的方法scope或namespace途径在config/routes.rb中文件中定义的,对不对?
我的命名空间路由如下所示:
namespace :api, constraints: { format: :json } do
devise_for :users, skip: [ :registrations, :passwords, :confirmations ]
resources :profiles, only: :show
end
Run Code Online (Sandbox Code Playgroud)
它的工作原理!
我做了什么让它发挥作用?答案在于config/initializers/devise.rb文件.检查文件底部附近它说...
# When using omniauth, Devise cannot automatically set Omniauth path,
# so you need to do it manually. For the users scope, it would be:
下一个注释行显示一个示例,取消注释该行并根据您的需要对其进行修改,对于我的情况(即,对于我上面的命名空间路由)我有:
config.omniauth_path_prefix = "/api/users/auth"
Run Code Online (Sandbox Code Playgroud)
就是这样!....我做到了,一切都开始完美!
希望能帮助到你!