omniauth-devise错误:“验证失败:电子邮件已被占用” [英] omniauth-devise error: "Validation failed: Email has already been taken"
问题描述
- 我们正在尝试用omniauth设计创建一个rails 4应用程序:
- 使用google登录
- 使用链接登录
- 使用Twitter进行登录
在这里,我可以使用facebook,linkedin,twitter或者google帐号登录。但我的问题是:我的Google帐户电子邮件和linkedin电子邮件地址是一样的。然后用google登录,然后使用linkedin登录就是给我这个错误:
验证失败:电子邮件已被采取
这是一个问题,因为devise使用:unique =>
任何人都可以提供我很好的想法来处理这个错误?
我按照这些步骤,为我工作正常:
1。宝石文件
gem'omniauth-facebook','1.4.0'
gem'omniauth-twitter '
gem'omniauth-google-oauth2'
2。 config / route.rb
devise_for:users,controllers:{omniauth_callbacks:omniauth_callbacks}
3。链接
<%= link_to使用Facebook登录,user_omniauth_authorize_path(:facebook)%>
<%= link_to使用twitter登录,user_omniauth_authorize_path(:twitter)%>
<%= link_to使用google登录,user_omniauth_authorize_path(:google_oauth2)%>
4。控制器/ omniauth_callbacks_controller.rb
class OmniauthCallbacksController< Devise :: OmniauthCallbacksController
skip_before_filter:authenticate_user!
def all
user = User.from_omniauth(request.env [omniauth.auth],current_user)
如果user.persisted?
flash [:notice] =你成功登录!!
sign_in_and_redirect(user)
else
会话[devise.user_attributes] = user.attributes
redirect_to new_user_registration_url
end
end
def failure
super
end
alias_method:facebook,,all
alias_method:twitter,:all
alias_method:google_oauth2,all
end
5。添加必填字段和模型
rails g migration add_social_network_info_columns_to_users name image_url位置
#生成新模型授权
rails g模型授权user_id:整数提供者uid令牌秘密用户名
6。 model / User.rb
class User< ActiveRecord :: Base
需要securerandom
#包含默认设计模块。其他可用的是:
#:confirmable,:lockable,:timeoutable and:omniauthable
devise:database_authenticatable,:registerable,
:可恢复,可记忆,可追踪,可验证,
:omniauthable
has_many:授权
#omniauth facebook提供者
def self.from_omniauth(auth,current_user)
#检查现有授权
#通过以下方式查找或创建授权:provider,uid,token和secret
authorization = Authorization.where(
:provider => auth.provider,
:uid => auth。 uid.to_s,
:token => auth.credentials.token,
:secret => auth.credentials.secret
).first_or_initialize
如果授权.user.blank?
user = current_user.nil? ? user.where('email =?',auth [info] [email])。first:current_user
#在用户表中保存用户相关数据
如果user.blank ?
User.new(
:email => auth.info.email,
:password => Devise.friendly_token [0,10],
:name => auth.info.name,
:locations => auth.info.location,
:image_url => auth.info.image
)
#since twitter不提供电子邮件,
#,所以你需要跳过twitter验证。
auth.provider ==twitter? user.save!(:validate => false):user.save!
end
#授权表中的商店授权相关数据
authorization.username = auth.info.nickname
authorization.user_id = user.id
授权。保存!
end
authorization.user
end
end
的 6。 model / Authorization.rb
类授权< ActiveRecord :: Base
belongs_to:user
end
来源:
https://github.com/mohitjain/social-login -in-rails
I am trying to create a rails 4 app with omniauth devise :
- signin with facebook
- signin with google
- signin with linkedin
- signin with twitter
Here, I am able to login in with either facebook, linkedin, twitter or google account. But my problem is: my google account email and linkedin email address are same. And login with google and then login with linkedin is giving me this error:
"Validation failed: Email has already been taken"
This is a problem because devise uses :unique => true
in migration file for email field.
Can anyone provide me nice idea to handle this error please?
I followed these steps and working fine for me:
1. Gemfile
gem 'omniauth-facebook', '1.4.0'
gem 'omniauth-twitter'
gem 'omniauth-google-oauth2'
2. config/route.rb
devise_for :users, controllers: { omniauth_callbacks: "omniauth_callbacks" }
3. Links
<%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>
<%= link_to "Sign in with twitter", user_omniauth_authorize_path(:twitter) %>
<%= link_to "Sign in with google", user_omniauth_authorize_path(:google_oauth2) %>
4. controllers/omniauth_callbacks_controller.rb
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
skip_before_filter :authenticate_user!
def all
user = User.from_omniauth(request.env["omniauth.auth"], current_user)
if user.persisted?
flash[:notice] = "you are successfully logged in!!"
sign_in_and_redirect(user)
else
session["devise.user_attributes"] = user.attributes
redirect_to new_user_registration_url
end
end
def failure
super
end
alias_method :facebook, :all
alias_method :twitter, :all
alias_method :google_oauth2, :all
end
5. add required fields and model
rails g migration add_social_network_info_columns_to_users name image_url locations
# generate new model Authorization
rails g model Authorization user_id:integer provider uid token secret username
6. models/User.rb
class User < ActiveRecord::Base
require 'securerandom'
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable
has_many :authorizations
# omniauth facebook provider
def self.from_omniauth(auth, current_user)
# check for existing authorization
# Find or create Authorization with: provider, uid, token and secret
authorization = Authorization.where(
:provider => auth.provider,
:uid => auth.uid.to_s,
:token => auth.credentials.token,
:secret => auth.credentials.secret
).first_or_initialize
if authorization.user.blank?
user = current_user.nil? ? User.where('email = ?', auth["info"]["email"]).first : current_user
# save user related data in user table
if user.blank?
User.new(
:email => auth.info.email,
:password => Devise.friendly_token[0,10],
:name => auth.info.name,
:locations => auth.info.location,
:image_url => auth.info.image
)
# since twitter don't provide email,
# so you need to skip validation for twitter.
auth.provider == "twitter" ? user.save!(:validate => false) : user.save!
end
# store authorization related data in authorization table
authorization.username = auth.info.nickname
authorization.user_id = user.id
authorization.save!
end
authorization.user
end
end
6. model/Authorization.rb
class Authorization < ActiveRecord::Base
belongs_to :user
end
这篇关于omniauth-devise错误:“验证失败:电子邮件已被占用”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!