Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Omniauth Facebook登录重定向到注册(如果用户存在)_Ruby On Rails_Ruby On Rails 4_Devise_Omniauth_Omniauth Facebook - Fatal编程技术网

Ruby on rails Omniauth Facebook登录重定向到注册(如果用户存在)

Ruby on rails Omniauth Facebook登录重定向到注册(如果用户存在),ruby-on-rails,ruby-on-rails-4,devise,omniauth,omniauth-facebook,Ruby On Rails,Ruby On Rails 4,Devise,Omniauth,Omniauth Facebook,我正在玩omniauth facebookgem,通过facebook帐户登录designe会话。当我点击“使用facebook登录”链接时,一切都进行得很顺利:创建了一个新帐户,我登录并返回主页,并显示一条消息确认我的新会话(非常好!) 问题:但是,当帐户已经存在时,单击链接,我将被重定向到用户/注册页面。我一直在关注Desive wiki。有大量关于类似错误的文档,以及。然而,每个解决方案都已经在我的应用程序中实现了(据我所知),或者(在最后一个链接的情况下)似乎基于一个旧的配置模型,该配置

我正在玩
omniauth facebook
gem,通过facebook帐户登录designe会话。当我点击“使用facebook登录”链接时,一切都进行得很顺利:创建了一个新帐户,我登录并返回主页,并显示一条消息确认我的新会话(非常好!)

问题:但是,当帐户已经存在时,单击链接,我将被重定向到
用户/注册
页面。我一直在关注Desive wiki。有大量关于类似错误的文档,以及。然而,每个解决方案都已经在我的应用程序中实现了(据我所知),或者(在最后一个链接的情况下)似乎基于一个旧的配置模型,该配置模型似乎与wiki完全不同,我不确定它是否适用

我最好的猜测是它与回调控制器有关,因为
@user.persistend?
似乎是错误的。这让我相信我对
@user
的定义是不正确的。见下文:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    logger.debug "Inside facebook"
    # You need to implement the method below in your model (e.g. app/models/user.rb)
    @user = User.from_omniauth(request.env["omniauth.auth"])
    logger.debug "User is #{@user}"

    if @user.persisted?
      logger.debug "@user.persisted?"
      debugger
      sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
      logger.debug "user exists"
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end

  def failure
    redirect_to root_path, alert: "Login failed"
  end
end
class Users::OmniAuthCallbackController:authentication#如果未激活@user,将引发此错误
如果是导航格式,设置flash消息(:notice,:success,:kind=>“Facebook”)?
logger.debug“用户存在”
其他的
会话[“designe.facebook_data”]=request.env[“omniauth.auth”]
将\重定向到新\用户\注册\ url
结束
结束
def故障
重定向到根路径,警告:“登录失败”
结束
结束
此外,我的用户模型如下:

class User < ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
                 :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:facebook]

    def self.from_omniauth(auth)
        where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
            user.provider = Devise.friendly_token[0,20]
            user.email = auth.info.email
            user.password = Devise.friendly_token[0,20]
            user.fname = auth.info.first_name
            user.lname = auth.info.last_name
        end
    end
end
class用户[:facebook]
定义自授权(授权)
其中(provider:auth.provider,uid:auth.uid)。首先|
user.provider=design.friendly_令牌[0,20]
user.email=auth.info.email
user.password=design.friendly_令牌[0,20]
user.fname=auth.info.first\u name
user.lname=auth.info.last\u name
结束
结束
结束

欢迎提出任何建议!提前感谢。

来自\u omniauth
从未找到现有的facebook用户,因为您正在覆盖提供商属性:

where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
在这种情况下,使用提供商“facebook”搜索用户,但找不到:

user.provider = Devise.friendly_token[0,20]
将提供程序更改为某个随机令牌


只需删除该行,它就可以正常工作了

从_omniauth
永远找不到现有的facebook用户,因为您正在覆盖提供者属性:

where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
在这种情况下,使用提供商“facebook”搜索用户,但找不到:

user.provider = Devise.friendly_token[0,20]
将提供程序更改为某个随机令牌


只要删除该行,它就会正常工作

class Authentication < ActiveRecord::Base

    belongs_to :user
    # validates :provider, :uid, :presence => true

    def self.from_omniauth(auth)
     authenticate = where(provider: auth[:provider], :uid=>auth[:uid]).first_or_initialize
        if authenticate.user
          authenticate.provider = auth[:provider]
          authenticate.uid =auth[:uid]
        else
            user = User.find_or_initialize_by(:email => email)
            authenticate.provider = auth[:provider]
            user.email = email
            user.first_name = first_name
            user.last_name = last_name
            user.social_image = image
            user.password =  Devise.friendly_token.first(8)
            user.save(validate: false)
            if user.errors.any?
              return user
            else
            authenticate.user_id = user.id
            end
         end
        authenticate.save
        authenticate.user

      end
    end
类身份验证true
定义自授权(授权)
authenticate=where(provider:auth[:provider],:uid=>auth[:uid])。首先\u或\u初始化
如果是,请验证.user
authenticate.provider=auth[:provider]
authenticate.uid=auth[:uid]
其他的
用户=用户。通过(:email=>email)查找或初始化
authenticate.provider=auth[:provider]
user.email=电子邮件
user.first\u name=first\u name
user.last\u name=last\u name
user.social_image=image
user.password=design.friendly_token.first(8)
user.save(验证:false)
如果user.errors.any?
返回用户
其他的
authenticate.user\u id=user.id
结束
结束
验证。保存
验证用户身份
结束
结束

试试这样的方法

class Authentication < ActiveRecord::Base

    belongs_to :user
    # validates :provider, :uid, :presence => true

    def self.from_omniauth(auth)
     authenticate = where(provider: auth[:provider], :uid=>auth[:uid]).first_or_initialize
        if authenticate.user
          authenticate.provider = auth[:provider]
          authenticate.uid =auth[:uid]
        else
            user = User.find_or_initialize_by(:email => email)
            authenticate.provider = auth[:provider]
            user.email = email
            user.first_name = first_name
            user.last_name = last_name
            user.social_image = image
            user.password =  Devise.friendly_token.first(8)
            user.save(validate: false)
            if user.errors.any?
              return user
            else
            authenticate.user_id = user.id
            end
         end
        authenticate.save
        authenticate.user

      end
    end
类身份验证true
定义自授权(授权)
authenticate=where(provider:auth[:provider],:uid=>auth[:uid])。首先\u或\u初始化
如果是,请验证.user
authenticate.provider=auth[:provider]
authenticate.uid=auth[:uid]
其他的
用户=用户。通过(:email=>email)查找或初始化
authenticate.provider=auth[:provider]
user.email=电子邮件
user.first\u name=first\u name
user.last\u name=last\u name
user.social_image=image
user.password=design.friendly_token.first(8)
user.save(验证:false)
如果user.errors.any?
返回用户
其他的
authenticate.user\u id=user.id
结束
结束
验证。保存
验证用户身份
结束
结束
试试这个

  def after_sign_in_path_for(resource)
       super resource
  end
从我所了解到的情况来看,您不打算进入登录页

  def after_sign_in_path_for(resource)
       super resource
  end

从我所了解到的情况来看,您不会进入登录页

您的项目中的任何地方都有用于方法的签名后路径方法吗?您的项目中的任何地方都有用于方法的签名后路径方法吗?