简体   繁体   English

设计Omniauth-Facebook值得记住

[英]Devise Omniauth-Facebook rememberable

Problem 问题

I have Devise Omniauth-Facebook authentication. 我有Devise Omniauth-Facebook身份验证。 The log in with facebook works, but the Session is lost when the user goes to localhost:3000 . 使用facebook登录有效,但当用户转到localhost:3000时会话丢失。 I have the following GEMs : 我有以下GEM

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

Description 描述

The Session works correctly for users not authenticated with Omniauth-Facebook, 会话适用于未通过Omniauth-Facebook验证的用户,

This is my devise.rb omniauth-facebook settings : 这是我的devise.rb omn​​iauth-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 

I already tried the following solution that did not work: 我已经尝试了以下不起作用的解决方案:

  1. turning off protect_from_forgery 关闭protect_from_forgery
  2. OmniAuth.config.full_host = " http://127.0.0.1:3000 " OmniAuth.config.full_host =“ http://127.0.0.1:3000
  3. Following the accepted solution of Jeroen van Dijk at the following post: Devise and OmniAuth remembering OAuth For this solution, in my rake routes I do not have the path user_oauth_connect_path , even if I added the route in routes.rb . 遵循Jeroen van Dijk在以下帖子中接受的解决方案: Devise和OmniAuth记住OAuth对于此解决方案,在我的rake路由中,我没有路径user_oauth_connect_path ,即使我在routes.rb添加了路由。 I also think this is not the solution to my problem because I have Devise 4.2.0 and Rails 5 我也认为这不是我的问题的解决方案,因为我有Devise 4.2.0和Rails 5
  4. @user.remember_me = true @ user.remember_me = true

All the previous solutions were taken from the following stackoverflow discussions: 以前的所有解决方案都来自以下stackoverflow讨论:

Omniauth+Facebook lost session Omniauth + Facebook失去了会话

Devise and OmniAuth remembering OAuth Devise和OmniAuth记住OAuth

The code is the standard one included in the guides from github of Devise and omniauth-facebook Thanks a lot for your help Fabrizio Bertoglio 代码是包含在Deviseomn​​iauth-facebook的 github指南中的标准代码非常感谢您的帮助Fabrizio Bertoglio

Maybe this is the solution to my problem? 也许这是我的问题的解决方案? Facebook login right now works and If the session is not stored, the user can login back again without problems. Facebook登录现在正常工作,如果没有存储会话,用户可以再次登录而不会出现问题。 I did not have any more experiences about losing the session so I am not taking so much interest in this issue. 我没有任何关于失去会议的经验,所以我对这个问题没有太大的兴趣。

Notice that Devise's RegistrationsController by default calls User.new_with_session before building a resource. 请注意,Devise的RegistrationsController默认在构建资源之前调用User.new_with_session。 This means that, if we need to copy data from session whenever a user is initialized before sign up, we just need to implement new_with_session in our model. 这意味着,如果我们需要在注册前初始化用户时从会话中复制数据,我们只需要在模型中实现new_with_session。 Here is an example that copies the facebook email if available: 这是一个复制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

Finally, if you want to allow your users to cancel sign up with Facebook, you can redirect them to cancel_user_registration_path. 最后,如果您希望允许用户取消使用Facebook注册,您可以将其重定向到cancel_user_registration_path。 This will remove all session data starting with devise. 这将删除以devise开头的所有会话数据。 and the new_with_session hook above will no longer be called. 并且将不再调用上面的new_with_session挂钩。

Omniauth Facebook Gihub page Omniauth Facebook Gihub页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM