设计Omniauth-Facebook记得 [英] Devise Omniauth-Facebook rememberable

查看:197
本文介绍了设计Omniauth-Facebook记得的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Devise Omniauth-Facebook 认证。使用Facebook登录工作,但当用户进入 localhost:3000 时,会话丢失。
我有以下 GEMs

I have Devise Omniauth-Facebook authentication. The log in with facebook works, but the Session is lost when the user goes to localhost:3000. I have the following GEMs:

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



描述



会话正确地为没有通过Omniauth-Facebook认证的用户

Description

The Session works correctly for users not authenticated with Omniauth-Facebook,

这是我的 devise.rb omniauth-facebook 设置

This is my devise.rb omniauth-facebook settings:

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. 关闭protect_from_forgery

  2. OmniAuth.config.full_host = http://127.0.0.1:3000

  3. 遵循Jeroen va的接受解决方案n Dijk在以下帖子:
    设计和OmniAuth记住OAuth
    对于这个解决方案,在我的rake路由中,我没有路径 user_oauth_connect_path ,即使我在 routes.rb 。我也认为这不是我的问题的解决方案,因为我有Devise 4.2.0和Rails 5

  4. @ user.remember_me = true

  1. turning off protect_from_forgery
  2. 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. I also think this is not the solution to my problem because I have Devise 4.2.0 and Rails 5
  4. @user.remember_me = true

所有以前的解决方案都是从以下stackoverflow讨论中获取的:

All the previous solutions were taken from the following stackoverflow discussions:

Omniauth + Facebook失去了会话

Devise和OmniAuth记住OAuth

代码是github指南中包含的标准 omniauth-facebook
非常感谢您的帮助
Fabrizio Bertoglio

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

推荐答案

也许这是我的问题的解决方案?现在Facebook登录正常工作,如果会话没有存储,用户可以重新登录,没有问题。我没有任何关于失去会话的经验,所以我对这个问题没有太多的兴趣。

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. I did not have any more experiences about losing the session so I am not taking so much interest in this issue.


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

Notice that Devise's RegistrationsController by default calls User.new_with_session before building a resource. 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. Here is an example that copies the facebook email if available:



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。这将删除从devise开始的所有会话数据。上面的new_with_session钩子将不再被调用。

Finally, if you want to allow your users to cancel sign up with Facebook, you can redirect them to cancel_user_registration_path. This will remove all session data starting with devise. and the new_with_session hook above will no longer be called.

Omniauth Facebook Gihub页面

这篇关于设计Omniauth-Facebook记得的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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