More writing role changes in sessions controller

This commit is contained in:
Fosco Marotto 2021-02-14 23:21:53 -05:00
parent 5e7cec5345
commit 7bfe6c7709

View File

@ -12,17 +12,21 @@ class Auth::SessionsController < Devise::SessionsController
before_action :set_body_classes before_action :set_body_classes
def new def new
Devise.omniauth_configs.each do |provider, config| ActiveRecord::Base.connected_to(role: :writing) do
return redirect_to(omniauth_authorize_path(resource_name, provider)) if config.strategy.redirect_at_sign_in Devise.omniauth_configs.each do |provider, config|
end return redirect_to(omniauth_authorize_path(resource_name, provider)) if config.strategy.redirect_at_sign_in
end
super super
end
end end
def create def create
super do |resource| ActiveRecord::Base.connected_to(role: :writing) do
remember_me(resource) super do |resource|
flash.delete(:notice) remember_me(resource)
flash.delete(:notice)
end
end end
end end
@ -36,13 +40,15 @@ class Auth::SessionsController < Devise::SessionsController
protected protected
def find_user def find_user
if session[:otp_user_id] ActiveRecord::Base.connected_to(role: :writing) do
User.find(session[:otp_user_id]) if session[:otp_user_id]
elsif user_params[:email] User.find(session[:otp_user_id])
if use_seamless_external_login? && Devise.check_at_sign && user_params[:email].index('@').nil? elsif user_params[:email]
User.joins(:account).find_by(accounts: { username: user_params[:email] }) if use_seamless_external_login? && Devise.check_at_sign && user_params[:email].index('@').nil?
else User.joins(:account).find_by(accounts: { username: user_params[:email] })
User.find_for_authentication(email: user_params[:email]) else
User.find_for_authentication(email: user_params[:email])
end
end end
end end
end end
@ -74,30 +80,36 @@ class Auth::SessionsController < Devise::SessionsController
end end
def valid_otp_attempt?(user) def valid_otp_attempt?(user)
user.validate_and_consume_otp!(user_params[:otp_attempt]) || ActiveRecord::Base.connected_to(role: :writing) do
user.invalidate_otp_backup_code!(user_params[:otp_attempt]) user.validate_and_consume_otp!(user_params[:otp_attempt]) ||
user.invalidate_otp_backup_code!(user_params[:otp_attempt])
end
rescue OpenSSL::Cipher::CipherError => _error rescue OpenSSL::Cipher::CipherError => _error
false false
end end
def authenticate_with_two_factor def authenticate_with_two_factor
user = self.resource = find_user ActiveRecord::Base.connected_to(role: :writing) do
user = self.resource = find_user
if user_params[:otp_attempt].present? && session[:otp_user_id] if user_params[:otp_attempt].present? && session[:otp_user_id]
authenticate_with_two_factor_via_otp(user) authenticate_with_two_factor_via_otp(user)
elsif user&.valid_password?(user_params[:password]) elsif user&.valid_password?(user_params[:password])
prompt_for_two_factor(user) prompt_for_two_factor(user)
end
end end
end end
def authenticate_with_two_factor_via_otp(user) def authenticate_with_two_factor_via_otp(user)
if valid_otp_attempt?(user) ActiveRecord::Base.connected_to(role: :writing) do
session.delete(:otp_user_id) if valid_otp_attempt?(user)
remember_me(user) session.delete(:otp_user_id)
sign_in(user) remember_me(user)
else sign_in(user)
flash.now[:alert] = I18n.t('users.invalid_otp_token') else
prompt_for_two_factor(user) flash.now[:alert] = I18n.t('users.invalid_otp_token')
prompt_for_two_factor(user)
end
end end
end end