gpt4 book ai didi

ruby-on-rails - Rails - 当 Omniauth 集成不存在时生成电子邮件地址

转载 作者:行者123 更新时间:2023-11-29 12:21:57 25 4
gpt4 key购买 nike

我正在使用带有 Rails 的 omniauth 并尝试让 twitter、facebook 和 google 连接起来进行身份验证,但一直遇到此错误:

PG::Error: ERROR:  duplicate key value violates unique constraint "index_users_on_email"
DETAIL: Key (email)=() already exists.

这是我的身份验证 Controller :

class AuthorizationsController < ApplicationController


def create
authentication = Authorization.find_by_provider_and_uid(auth['provider'], auth['uid'])

if authentication
flash[:notice] = "Signed In Successfully"
sign_in authentication.user, event: :authentication
redirect_to root_path
else
athlete = Athlete.new
athlete.apply_omniauth(auth)

debugger

if athlete.save(validate: false)
flash[:notice] = "Account created and signed in successfully"
sign_in athlete, event: :authentication
redirect_to finalize_profile_path
else
flash[:error] = "An error has occurred. Please try again."
redirect_to root_path
end
end
end

def failure
render json: params.to_json
end

private

def auth
request.env["omniauth.auth"]
end

def resource(user_type)
user_type.downcase.to_sym
end

end

我认为发生的事情是,当创建运动员时,它正在创建一个带有空白电子邮件地址的运动员,并且唯一 key 失败了……我该如何解决这个问题?我想我知道如何针对 Google 集成解决此问题,但由于 Twitter 不返回电子邮件,因此此问题不会自行解决

最佳答案

这就是我如何让它工作的:

class AuthorizationsController < ApplicationController

def create
authentication = Authorization.find_by_provider_and_uid(auth['provider'], auth['uid'])

if authentication
flash[:notice] = "Signed In Successfully"
sign_in authentication.user, event: :authentication
redirect_to root_path
else
athlete = Athlete.new(email: generate_auth_email(params[:provider]) )
athlete.apply_omniauth(auth)

debugger

if athlete.save(validate: false)
flash[:notice] = "Account created and signed in successfully"
sign_in athlete, event: :authentication
redirect_to finalize_profile_path
else
flash[:error] = "An error has occurred. Please try again."
redirect_to root_path
end
end
end

def failure
render json: params.to_json
end

private

def auth
request.env["omniauth.auth"]
end

def resource(user_type)
user_type.downcase.to_sym
end

def generate_auth_email(provider)
return auth.info.try(:email) unless provider == "twitter"
return "#{auth.uid}@twitter.com" if provider == "twitter"
end

end

我使用 twitter uid 创建电子邮件,twitter.com 是域,因为 twitter 不返回电子邮件地址

希望这对以后的人有帮助

关于ruby-on-rails - Rails - 当 Omniauth 集成不存在时生成电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17579944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com