Devise Omniauth,如何链接谷歌帐户
我已经设置了与omniauth 一起工作的设计。 devise.rb 是这样的:
...
config.omniauth :facebook, FACEBOOK_APP_ID, FACEBOOK_APP_SECRET, :scope => FACEBOOK_APP_PERMISSIONS
config.omniauth :openid, OpenID::Store::Filesystem.new('./tmp'), :name => 'yahoo', :identifier => 'yahoo.com'
config.omniauth :openid, OpenID::Store::Filesystem.new('./tmp'), :name => 'gmail', :identifier => 'https://www.google.com/accounts/o8/id'
...
我想将现有帐户与上述 3 个帐户链接起来,并使用回调控制器中的以下代码:
...
def callback(provider)
if utilizator_signed_in?
# link it's account
if Login.link_omniauth(current_utilizator, omniauth_data)
flash[:notice] = I18n.t "devise.omniauth_callbacks.link.success", :kind => provider
redirect_to :root
end
else
utilizator = Login.auth_with_omniauth(omniauth_data)
if !utilizator.nil?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => provider
sign_in_and_redirect utilizator, :event => :authentication
else
utilizator = Login.register_omniauth(omniauth_data)
flash[:notice] = I18n.t "devise.omniauth_callbacks.register.success", :kind => provider
sign_in_and_redirect utilizator, :event => :authentication
end
end
end
...
它与 facebook 配合得很好,但对于 google 和 yahoo,当前用户已注销并且创建一个新用户。
如何跳过用户退出阶段?
谢谢,
编辑: 我使用的是最新版本3.0.rc3。 Login 的函数很容易猜到: link_omniauth - 将当前登录用户链接到帐户 auth_with_omniauth - 将验证用户 register_omniauth - 将注册一个新用户
这里的问题是utilizator_signed_in? (user_signed_in?) 当用户登录时,Google 会返回 false,我认为之前发生了sign_out,而 facebook 则没有发生。
I've set up devise to work with omniauth and. This is how devise.rb looks like:
...
config.omniauth :facebook, FACEBOOK_APP_ID, FACEBOOK_APP_SECRET, :scope => FACEBOOK_APP_PERMISSIONS
config.omniauth :openid, OpenID::Store::Filesystem.new('./tmp'), :name => 'yahoo', :identifier => 'yahoo.com'
config.omniauth :openid, OpenID::Store::Filesystem.new('./tmp'), :name => 'gmail', :identifier => 'https://www.google.com/accounts/o8/id'
...
I want to link an existing account with the the above 3, with the following code from the callback controller:
...
def callback(provider)
if utilizator_signed_in?
# link it's account
if Login.link_omniauth(current_utilizator, omniauth_data)
flash[:notice] = I18n.t "devise.omniauth_callbacks.link.success", :kind => provider
redirect_to :root
end
else
utilizator = Login.auth_with_omniauth(omniauth_data)
if !utilizator.nil?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => provider
sign_in_and_redirect utilizator, :event => :authentication
else
utilizator = Login.register_omniauth(omniauth_data)
flash[:notice] = I18n.t "devise.omniauth_callbacks.register.success", :kind => provider
sign_in_and_redirect utilizator, :event => :authentication
end
end
end
...
It works just great with facebook, but for google and yahoo the current user get's signed out and a new user is created.
How can I skip the user sign_out phase ?
Thank you,
Edit:
I am using the latest version 3.0.rc3.
The functions from Login are easy to guess:
link_omniauth - will link the current logged user to a account
auth_with_omniauth - will auth the user
register_omniauth - will register a new user
The problem here is that utilizator_signed_in? (user_signed_in?) will return false for google when the user is signed in, I think there is a prior sign_out happening that is not happening for facebook.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我强烈推荐这个教程:这里(比 Rails 转换和其他教程更深入)主题)。特别是,它有一个全面的 200 行代码 (
services_controller.rb
),用于创建您需要使用 Omniauth 高效处理任何身份验证服务 (Twitter/Facebook/Google/Github) 的控制器,并将其链接到预先存在的 Devise 帐户,或创建新帐户。我有一个(几乎)实时项目正在运行这里 -
您可以使用 Facebook/Twitter 登录(我暂时没有启用 google/github),登录后如果转到“个人资料”->“服务”,它会向您显示与您的帐户关联的多个身份验证服务。
我不愿意上传我的项目的源代码,因为它有很多与此无关的其他代码,但如果您确实觉得需要它,请告诉我。
I highly recommend this tutorial: here (far more in-depth than the rails casts and other tutorials on the topic). In particular, it has a comprehensive 200 line piece of code (
services_controller.rb
) to create the controller that you will need to handle any authentication service (Twitter/Facebook/Google/Github) efficiently using Omniauth, and either link it to pre-existing Devise accounts, or create new accounts.I have a (almost) live project running with that here -
You can sign in using Facebook/Twitter (I haven't enabled google/github for now), and if you go to Profile->Services after you are signed in, it will show you the multiple authentication services linked to your account.
I'm reluctant to upload my project's source since it has a lot of other code that isn't relevant to this at all, but if you really feel like you need it, then tell me.
所以我终于找到了我的问题的答案。
这个人 https://github.com/intridea/omniauth/issues/185 有同样的问题。
感谢您的回复,
So I finally found the answer to my question.
This guy https://github.com/intridea/omniauth/issues/185 had the same issue.
Thanks for your replies,
我希望已经将我的个人资料任务的 Gmail 地址链接提交到我的申请中
I hope already submit my gmail address link to my profile task into my application