在 omniauth 中命名空间的 Rails 路由 [英] Rails route namespaced in omniauth
问题描述
我在 routes.rb 中有这个:
I have this in routes.rb:
namespace :api do
namespace :v1 do
...
devise_for :users, constraints: { format: :json },
:controllers => { :omniauth_callbacks => "auths" }
...
end
end
并产生以下路线:
new_api_v1_user_confirmation GET /api/v1/users/confirmation/new(.:format) api/v1/confirmations#new {:format=>:json}
GET /api/v1/users/confirmation(.:format) api/v1/confirmations#show {:format=>:json}
api_v1_user_omniauth_authorize /users/auth/:provider(.:format) auths#passthru {:provider=>/facebook|twitter|linkedin/, :format=>:json}
api_v1_user_omniauth_callback /users/auth/:action/callback(.:format) auths#(?-mix:facebook|twitter|linkedin) {:format=>:json}
如何获得命名空间的最后两条路由,例如:
How could a get last two routes namespaced, something like:
/api/v1/auth/:provider(.:format)
/api/v1/auth/:provider/callback(.:format)
推荐答案
我想我应该将我的评论转换为答案:
Guess I should convert my comments into an answer:
对于我们的应用程序,我们正在使用主干/牵线木偶做纯 json api 的事情.为了让 oAuth 与设计一起工作 - 我从设计中删除了它.:) 删除了我设置的 omniauthable 属性,并从我的 initializers/devise.rb 中删除了 omniauth 设置.然后在 omniauth 页面上阅读我自己实现的.
For our app we are doing the pure json api thing, with backbone/marionette. To get oAuth working with devise - I removed it from devise. :) Removed the omniauthable property I had set up and removed the omniauth settings from my initializers/devise.rb. Then reading on the omniauth page I implemented it by itself.
我的 api 位于/api/v1"下
My api lives under "/api/v1"
- 创建了 initializers/omniauth.rb 文件,其中列出了我的提供者和密钥.对于每个提供者,我还给了它一个 :path_prefix=>"/api/v1/auth" 属性.
- 在我的 api 中创建一个名为 api/v1/oauth_controller.rb 的回调控制器,它使用模块正确命名并包含我的服务回调路径.
- 更新了我的路由以设置 omni 的回调路由.请参阅此处:gist.github.com/DaveSanders/5835642
- 在 OAuthController.create 中,我使用了来自提供者的详细信息,并完成了社交网络用户是否存在并拥有映射帐户?"的基本流程.如果是这样,请通过设备的 user.sign_in 登录?如果没有,请创建用户,然后让他们登录.
- 重定向回我的应用,然后再次启动主干,然后可以获取登录的用户详细信息并根据需要使用它们.
您的实现可能会有所不同,但我处理我的 oAuth 帐户的方式是将它们放在他们自己的表(Twitter、Facebook 等)中,然后将它们链接到我的设计用户.这样我就可以关联多个帐户,并且用户可以使用其中的任何一个登录.
Your implementation may vary, but the way I handle my oAuth accounts is put them in their own tables (Twitters, Facebooks, etc) and then link them into my devise user. This way I can have multiple accounts associated and the user can log in with any of them.
此外,请务必将您的 twitter/facebook 回调设置为:
Also, be sure to set your twitter/facebook callback to something like:
http://127.0.0.1:3000/api/v1/auth/twitter/callback
匹配你在开发中的路线.
to match your route in dev.
希望这对其他人有帮助.如果我忘记了一步或者你迷路了,请询问.
Hope this helps others. If I forgot a step or you get lost, please ask.
这篇关于在 omniauth 中命名空间的 Rails 路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!