在过去的两天里,我花了大部分时间让它开始工作,但它终于起作用了,但我觉得它是极其多余的。
我正在为一个名为“授权”的表遵循一种belongs_to方法--我希望用户能够通过电子邮件注册,或者我实现的任何有电子邮件的Omniauth通道(我会给它一个设计密码),并自由地链接/未链接授权的应用程序。
class AuthenticationsController < ApplicationController
skip_before_action :verify_authenticity_token, :only => :steam
def index
@authentications = current_user.authentications.all
end
# <%= link_to 'Authentications', authentications_path %>
def home
end
def twitter
omni = request.env["omniauth.auth"]
authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid'])
if authentication
flash[:notice] = "Logged in Successfully"
sign_in_and_redirect User.find(authentication.user_id)
elsif current_user
token = omni['credentials'].token
token_secret = omni['credentials'].secret
current_user.authentications.create!(:provider => omni['provider'],
:uid => omni['uid'],
:token => token,
:token_secret => token_secret)
flash[:notice] = "Authentication successful."
sign_in_and_redirect current_user
else
user = User.new
user.apply_omniauth(omni)
if user.save
flash[:notice] = "Logged in."
sign_in_and_redirect User.find(user.id)
else
session[:omniauth] = omni.except('extra')
redirect_to new_user_registration_path
end
end
end
def destroy
@authentication = Authentication.find(params[:id])
@authentication.destroy
redirect_to authentications_url, :notice => "Successfully destroyed authentication."
end
def facebook
omni = request.env["omniauth.auth"]
authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid'])
if authentication
flash[:notice] = "Logged in Successfully"
sign_in_and_redirect User.find(authentication.user_id)
elsif current_user
token = omni['credentials'].token
token_secret = ""
current_user.authentications.create!(:provider => omni['provider'],
:uid => omni['uid'],
:token => token,
:token_secret => token_secret)
flash[:notice] = "Authentication successful."
sign_in_and_redirect current_user
else
user = User.new
user.email = omni['extra']['raw_info'].email
user.apply_omniauth(omni)
if user.save
flash[:notice] = "Logged in."
sign_in_and_redirect User.find(user.id)
else
session[:omniauth] = omni.except('extra')
redirect_to new_user_registration_path
end
end
end
def steam
omni = request.env["omniauth.auth"]
authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid'])
if authentication
flash[:notice] = "Logged in Successfully"
sign_in_and_redirect User.find(authentication.user_id)
elsif current_user
token = omni['extra']['raw_info'].steamid
# render :text => request.env["omniauth.auth"].info.to_hash.inspect
puts token
token_secret = ""
current_user.authentications.create!(:provider => omni['provider'],
:uid => omni['uid'],
:token => token,
:token_secret => token_secret)
flash[:notice] = "Authentication successful."
sign_in_and_redirect current_user
else
user = User.new
user.apply_omniauth(omni)
end
if user.save
flash[:notice] = "Logged in."
sign_in_and_redirect User.find(user.id)
else
session[:omniauth] = omni.except('extra')
redirect_to new_user_registration_path
end
end
end这部分是非常多余的,尤其是当我昨晚做了更多的修改,最终得到的蒸汽甚至工作。
它在user model中使用的函数
def apply_omniauth(omni)
authentications.build(:provider => omni['provider'],
:uid => omni['uid'],
:token => omni['credentials'].token,
:token_secret => omni['credentials'].secret)
end
def password_required?
(authentications.empty? || !password.blank?) && super #&& provider.blank?
end
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
end
end我在为蒸汽公司填充假数据,就像我在推特上一样。
我终于让这些联接在每种形式上都起作用了,尽管最后的“否则”(不是注册,尝试新加入)并没有起作用,因为我希望在bug测试之前尽量减少冗余。
发布于 2018-08-27 20:56:15
这里有一个删除冗余的选项:将特定于提供程序的defs替换为一个单独的,如下所示:
def authlenticate
# I misspelled authlenticate above on purpose
# Steam requires a different handling of the authenticity token
omni = request.env["omniauth.auth"]
authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid'])
if authentication
flash[:notice] = "Logged in Successfully"
sign_in_and_redirect User.find(authentication.user_id)
elsif current_user
token = omni['credentials'].token || omni['extra']['raw_info'].steamid
# if steam produces a value for omni['credentials'].token then the above line will fail for steam
token_secret = omni['credentials'].secret || ""
# if omni['credentials'].secret is not provided (e.g. FB) then set it to ""
current_user.authentications.create!(:provider => omni['provider'],
:uid => omni['uid'],
:token => token,
:token_secret => token_secret)
flash[:notice] = "Authentication successful."
sign_in_and_redirect current_user
else
user = User.new
user.apply_omniauth(omni)
if user.save
flash[:notice] = "Logged in."
sign_in_and_redirect User.find(user.id)
else
session[:omniauth] = omni.except('extra')
redirect_to new_user_registration_path
end
end
endhttps://codereview.stackexchange.com/questions/169556
复制相似问题