【问题标题】:Devise Omniauth-Facebook rememberable设计 Omniauth-Facebook 可记忆
【发布时间】:2017-02-16 17:17:40
【问题描述】:

问题

我有 Devise Omniauth-Facebook 身份验证。使用 facebook 登录有效,但是当用户转到 localhost:3000 时会话丢失。 我有以下 GEM

Devise 4.2.0
Rails 5
omniauth 1.4.0
omniauth-facebook 4.0.0
omniauth-oauth2 1.4.0

说明

会话对未通过 Omniauth-Facebook 进行身份验证的用户正常工作,

这是我的devise.rbomniauth-facebook 设置

config.omniauth :facebook, "APP_ID", "APP_SECRET", callback_url: "http://127.0.0.1:3000/users/auth/facebook/callback", scope: 'public_profile, email', image_size: 'large', provider_ignores_state: true 

我已经尝试了以下不起作用的解决方案:

  1. 关闭protect_from_forgery
  2. OmniAuth.config.full_host = "http://127.0.0.1:3000"
  3. 遵循 Jeroen van Dijk 在以下帖子中接受的解决方案: Devise and OmniAuth remembering OAuth 对于这个解决方案,在我的 rake 路由中,我没有路径 user_oauth_connect_path,即使我在 routes.rb 中添加了路由。我也认为这不是我的问题的解决方案,因为我有 Devise 4.2.0 和 Rails 5
  4. @user.remember_me = true

之前的所有解决方案都取自以下stackoverflow讨论:

Omniauth+Facebook lost session

Devise and OmniAuth remembering OAuth

代码是 Deviseomniauth-facebook 的 github 指南中包含的标准代码 非常感谢你的帮助 法布里齐奥·贝托利奥

【问题讨论】:

  • 我刚刚发现了这个讨论,现在功能运行良好。如果我尝试实施此解决方案,我将发布答案。 coderwall.com/p/bsfitw/…

标签: ruby-on-rails devise ruby-on-rails-5 omniauth-facebook


【解决方案1】:

也许这就是我的问题的解决方案? Facebook 登录现在可以正常工作,如果会话未存储,用户可以再次登录而不会出现问题。我没有更多关于失去会话的经验,所以我对这个问题没有太大兴趣。

请注意,默认情况下,Devise 的 RegistrationsController 在构建资源之前会调用 User.new_with_session。这意味着,如果我们需要在注册前初始化用户时从会话中复制数据,我们只需要在我们的模型中实现 new_with_session。以下是复制 Facebook 电子邮件(如果有)的示例:

class User < ApplicationRecord
  def self.new_with_session(params, session)
    super.tap do |user|
      if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
        user.email = data["email"] if user.email.blank?
      end
    end
  end
end

最后,如果您想允许您的用户取消注册 Facebook,您可以将他们重定向到 cancel_user_registration_path。这将删除以设计开头的所有会话数据。并且上面的 new_with_session 钩子将不再被调用。

Omniauth Facebook Gihub page

【讨论】:

    猜你喜欢
    • 2011-07-30
    • 2014-05-23
    • 2012-03-05
    • 2016-06-10
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 2015-07-22
    • 2016-12-22
    相关资源
    最近更新 更多