feat: Add authentication templates and actions
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module EuchreCamp
|
||||
module Actions
|
||||
module Auth
|
||||
module Login
|
||||
class Create < EuchreCamp::Action
|
||||
include Deps[users_repo: 'repositories.users', players_repo: 'repositories.players']
|
||||
|
||||
def handle(request, response)
|
||||
email = request.params[:email]
|
||||
password = request.params[:password]
|
||||
|
||||
user = users_repo.by_email(email)
|
||||
|
||||
if user && valid_password?(user, password)
|
||||
if user.locked?
|
||||
response.flash[:error] = 'Account is locked. Please contact support.'
|
||||
response.redirect_to '/login'
|
||||
return
|
||||
end
|
||||
|
||||
# Record successful login
|
||||
ip_address = request.env['REMOTE_ADDR']
|
||||
users_repo.record_login(user[:id], ip_address)
|
||||
|
||||
# Create session
|
||||
session = request.session
|
||||
session[:user_id] = user[:id]
|
||||
session[:player_id] = user[:player_id]
|
||||
|
||||
response.flash[:success] = 'Welcome back!'
|
||||
response.redirect_to '/rankings'
|
||||
else
|
||||
# Record failed login
|
||||
users_repo.record_failed_login(user[:id]) if user
|
||||
|
||||
response.flash[:error] = 'Invalid email or password'
|
||||
response.redirect_to '/login'
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def valid_password?(user, password)
|
||||
BCrypt::Password.new(user[:password_digest]) == password
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user