【发布时间】:2023-04-05 01:39:02
【问题描述】:
我正在使用omniauth-facebook 并在我的rails 4 应用程序中设计身份验证。
我想要一个已经通过设计进行身份验证的用户,如果他们选择,也可以稍后添加 facebook 身份验证。
我想知道如何检查设计登录用户的电子邮件是否与 facebook 用户的电子邮件匹配,如果是,请将 uid 和提供者添加到该注册用户。
目前,我收到设计错误消息:“电子邮件已被占用”
用户.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
谢谢!!!!
【问题讨论】:
标签: facebook ruby-on-rails-4 devise omniauth email-validation