三路omniauth 身份验证关系。如何设置呢?
我已经按照优秀的 Railscasts 在我正在开发的应用程序中实现了 facebook 和 twitter 身份验证有关该主题的视频。
到目前为止,我已经将我的用户模型与 Devise 连接起来,并且我构建了我的身份验证模型并记录了身份验证提供程序用户通过他们注册。它看起来像这样:
id | user_id | provider | uid
-----------------------------
1 | 1 | facebook | xyf
2 | 1 | twitter | pfr
3 | 2 | facebook | sdf
现在,我接下来需要的是存储这些omniauth 提供商提供给我的一些数据。例如,Facebook 提供用户性别及其 Facebook 个人资料的 URL。另一方面,与 Twitter 的连接将提供有关用户的不同信息。
这让我相信我应该有两张表,一张用于 twitter 数据 (TwitterProfile),一张用于 facebook 数据 (FacebookProfile)。
那么我应该像这样扩展我的身份验证模型吗?
id | user_id | provider | uid | facebook_profile_id | twitter_profile_id
------------------------------------------------------------------------
1 | 1 | facebook | xyf | 1 |
2 | 1 | twitter | pfr | | 2
3 | 2 | facebook | sdf | 2 |
我的 models/user.rb 看起来像这样:
has_many :authentications, :dependent => :destroy
has_one :facebook_profile, :through => :authentications, :source => :facebook_profile
has_one :twitter_profile, :through => :authentications, :source => :twitter_profile
但是我的身份验证模型可以属于 3 个表吗?
belongs_to :user
belongs_to :facebook_profile, :class_name => "FacebookProfile"
belongs_to :twitter_profile, :class_name => "TwitterProfile"
看起来我走错了路,因为我的身份验证模型中有多余的空间,而关系似乎过于复杂。
I've implemented facebook and twitter authentication in an app I'm working on by following the excellent Railscasts videos on the topic.
Thus far I have my user model hooked up with Devise and I have my Authentications model built and recording authentication providers when users sign up via them. It looks like this:
id | user_id | provider | uid
-----------------------------
1 | 1 | facebook | xyf
2 | 1 | twitter | pfr
3 | 2 | facebook | sdf
Now, the next thing I need is to store some of the data that these omniauth providers give me. Facebook for example provides the users gender and the url for their facebook profile. A connection to twitter on the other hand will provide different information on a user.
This leads me to believe that I should have two tables, one for twitter data (TwitterProfile) and one for facebook data (FacebookProfile).
So should I extend my authentications model like this?
id | user_id | provider | uid | facebook_profile_id | twitter_profile_id
------------------------------------------------------------------------
1 | 1 | facebook | xyf | 1 |
2 | 1 | twitter | pfr | | 2
3 | 2 | facebook | sdf | 2 |
and the my models/user.rb would look something like:
has_many :authentications, :dependent => :destroy
has_one :facebook_profile, :through => :authentications, :source => :facebook_profile
has_one :twitter_profile, :through => :authentications, :source => :twitter_profile
But can my authentications model belong to 3 tables?
belongs_to :user
belongs_to :facebook_profile, :class_name => "FacebookProfile"
belongs_to :twitter_profile, :class_name => "TwitterProfile"
It just seems like I'm on the wrong track because I have redundant space in my authentications model ant the relationships seem overly complicated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要考虑多态关联。假设该关联名为 Profile。基本上,关联有一个 profile_id 和一个字符串 profile_type 列,该列指示每个身份验证具有什么类型的配置文件。
有很多资源解释了如何进行设置,包括 Railscast 视频,这是Rails 指南 位于 Rails API 也是如此。
You might want to consider a polymorphic association. Say the association is called Profile. Basically, the association has a single profile_id, and a string profile_type column, which indicates what type of profile each authentication has.
There are lots of resources explaining how to set this up including a Railscast video, a section in the Rails Guides and it's in the Rails API too.