e7ddd0f72f
Add user authentication infrastructure with login/logout functionality, session management, and basic user model. Changes: - Create users table with email, password_digest, confirmation tokens - Add Users repository with authentication methods - Add User entity with authentication helpers - Implement login form and authentication actions - Add logout action - Add BCrypt gem for password hashing - Update base Action class with authentication helpers - Add login/logout routes Next steps: Registration flow, password reset, email confirmation
64 lines
1.8 KiB
Ruby
64 lines
1.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module EuchreCamp
|
|
module Actions
|
|
module Auth
|
|
class Login < EuchreCamp::Action
|
|
include Deps[
|
|
users_repo: 'repositories.users',
|
|
players_repo: 'repositories.players'
|
|
]
|
|
|
|
# Show login form (GET)
|
|
class Show < Login
|
|
def handle(request, response)
|
|
render_with_player(request, response)
|
|
end
|
|
end
|
|
|
|
# Process login (POST)
|
|
class Create < Login
|
|
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
|