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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.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
Omniauth facebook&;设计:如果存在电子邮件_Facebook_Ruby On Rails 4_Devise_Omniauth_Email Validation - Fatal编程技术网

Omniauth facebook&;设计:如果存在电子邮件

Omniauth facebook&;设计:如果存在电子邮件,facebook,ruby-on-rails-4,devise,omniauth,email-validation,Facebook,Ruby On Rails 4,Devise,Omniauth,Email Validation,我正在使用omniauth facebook和Desive在我的rails 4应用程序中进行身份验证。 我希望已经通过Desive进行身份验证的用户,如果愿意,也可以在以后添加facebook身份验证。 我想知道如何检查Desive登录用户的电子邮件是否与facebook用户的电子邮件匹配,如果匹配,则向该注册用户添加uid和提供者 目前,我收到Desive错误消息:“电子邮件已被接收” User.rb def self.from_omniauth(auth) where(auth.slic

我正在使用omniauth facebook和Desive在我的rails 4应用程序中进行身份验证。 我希望已经通过Desive进行身份验证的用户,如果愿意,也可以在以后添加facebook身份验证。
我想知道如何检查Desive登录用户的电子邮件是否与facebook用户的电子邮件匹配,如果匹配,则向该注册用户添加uid和提供者

目前,我收到Desive错误消息:“电子邮件已被接收

User.rb

def self.from_omniauth(auth)
  where(auth.slice(:provider, :uid)).first_or_create do |user|
    user.provider = auth.provider
    user.uid = auth.uid
    user.name = auth.info.name
    user.username = auth.info.username
    user.email = auth.info.email
    user.oauth_token = auth.credentials.token
    user.oauth_expires_at = Time.at(auth.credentials.expires_at)      
  end
end

def self.new_with_session(params, session)
  if session["devise.user_attributes"]
    new(session["devise.user_attributes"], without_protection: true) do |user|
      user.attributes = params
      user.valid?
    end
  else
    super
  end
end
控制器:

class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def all
    #render :text => "<pre>" + env["omniauth.auth"].to_yaml and return
    user = User.from_omniauth(request.env["omniauth.auth"])
    if user.persisted?
      #session[:user_id] = user.id # for current_user
      flash.notice = "Signed in!"
      sign_in_and_redirect user
    else
      session["devise.user_attributes"] = user.attributes
      redirect_to new_user_registration_url
    end
  end
  alias_method :facebook, :all
end
class OmniAuthCallbackController“”+env[“omniauth.auth”]。到_yaml并返回
def self.from_omniauth(auth)
  if self.where(email: auth.info.email).exists?
    return_user = self.where(email: auth.info.email).first
    return_user.provider = auth.provider
    return_user.uid = auth.uid
  else
    return_user = self.create do |user|
       user.provider = auth.provider
       user.uid = auth.uid
       user.name = auth.info.name
       user.username = auth.info.username
       user.email = auth.info.email
       user.oauth_token = auth.credentials.token
       user.oauth_expires_at = Time.at(auth.credentials.expires_at) 
    end
  end

  return_user
end
user=user.from_omniauth(request.env[“omniauth.auth”]) 如果user.com持久化? #会话[:user_id]=user.id#用于当前_用户 flash.notice=“已登录!” 登录并重定向用户 其他的 会话[“设计用户属性”]=用户属性 将\重定向到新\用户\注册\ url 结束 结束 别名_方法:facebook,:all 结束

谢谢

我只是遇到了同样的问题,并使用此代码来解决它

它首先检查数据库中是否存在电子邮件,如果存在,则更新uidprovider字段,如果不存在,则使用auth参数创建一个新用户。最后,我只返回变量return\u user,流程正常工作