Rails路线命名在omniauth中 [英] Rails route namespaced in omniauth

查看:109
本文介绍了Rails路线命名在omniauth中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在routes.rb中有这个:

 命名空间:api do 
命名空间:v1 do
...
devise_for:用户,约束:{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}

如何获得最后两条路线命名空间,如:

  / api / v1 / auth /:provider(。:format)
/api/v1/auth/:provider/callback(.:format)


决方案

猜猜我应该将我的意见转换成答案:



对于我们的应用程序,我们正在做纯json api的事情,用骨干/牵线木偶。要让oAuth与设计工作 - 我把它从设计中删除。 :)删除了我设置的全方位属性,并从我的初始化程序/ devise.rb中删除了omniauth设置。然后阅读Omniauth页面,我自己实现它。



我的api生活在/ api / v1下


  1. 创建了列出我的提供者和密钥的initializers / omniauth.rb文件。对于每个提供者,我还给了它一个:path_prefix =>/ api / v1 / auth属性。

  2. 在我的api中创建一个名为api / v1 / oauth_controller.rb的回调控制器正确命名为模块,并包含我从服务的回调路径。

  3. 更新了我的路由以设置omni的回调路由。参见这里:gist.github.com/DaveSanders/5835642

  4. 在OAuthController.create中我从供应商那里消费了详细信息,并通过社交网络用户是否存在并具有映射帐户?如果是这样,请通过devise的user.sign_in登录如果没有,创建用户然后登录。

  5. 重定向到我的应用程序,然后再次启动骨干,然后可以获取登录的用户详细信息并将其用作

您的实现可能会有所不同,但是我处理我的oAuth帐户的方式是将它们放在自己的表中(Twitters,Facebooks,等等),然后将它们链接到我的设计用户。这样,我可以有多个帐户相关联,用户可以使用任何一个帐户登录。



另外,请确保将您的twitter / facebook回调设置为: / p>

http://127.0.0.1:3000/api/v1/auth/twitter/callback



以匹配您的路径在开发。



希望这有助于别人。如果我忘记了一步,或者迷路,请问。


I have this in routes.rb:

namespace :api do
  namespace :v1 do
    ...
    devise_for :users, constraints: { format: :json },
      :controllers => { :omniauth_callbacks => "auths" }
    ...
  end
end

And produces among others, these routes:

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:

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.

My api lives under "/api/v1"

  1. Created the initializers/omniauth.rb file listing my providers and keys. For each provider I also gave it a :path_prefix=>"/api/v1/auth" property.
  2. Create a callback controller within my api called api/v1/oauth_controller.rb This was properly namespaced with modules and contains my callback path from the services.
  3. Updated my routes to setup the callback route for omni. See here: gist.github.com/DaveSanders/5835642
  4. Within OAuthController.create I consumed the details from the provider and go through the basic flow of "does the social network user exist and have a mapped account?" if so, log them in via devise's user.sign_in? If not, create the user and then sign them in.
  5. Redirect back to my app, which then boots up backbone again, which can then go get the logged in user details and use them as needed.

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.

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.

这篇关于Rails路线命名在omniauth中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆