feat: Add authentication templates and actions

This commit is contained in:
2026-03-27 13:09:46 -07:00
parent c5f6b2f6b3
commit 76a46f51ed
5 changed files with 144 additions and 0 deletions
+52
View File
@@ -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
+27
View File
@@ -0,0 +1,27 @@
# frozen_string_literal: true
module EuchreCamp
module Actions
module Auth
module Login
class Show < EuchreCamp::Action
include Deps[users_repo: 'repositories.users', players_repo: 'repositories.players']
def handle(request, response)
player = current_player(request)
response.render(view, current_player: player)
end
protected
def current_player(request)
player_id = request.session[:player_id]
return nil unless player_id
players_repo.by_id(player_id)
end
end
end
end
end
end
+39
View File
@@ -0,0 +1,39 @@
<div class="auth-container">
<div class="auth-card">
<h1>Login</h1>
<% if flash[:error] %>
<div class="alert alert-error"><%= flash[:error] %></div>
<% end %>
<% if flash[:success] %>
<div class="alert alert-success"><%= flash[:success] %></div>
<% end %>
<form action="/auth/login" method="POST">
<div class="form-group">
<label for="email">Email</label>
<input type="email" id="email" name="email" required autocomplete="email">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" required autocomplete="current-password">
</div>
<div class="form-group remember-me">
<label>
<input type="checkbox" name="remember" value="1">
Remember me
</label>
</div>
<button type="submit" class="btn btn-primary btn-block">Login</button>
</form>
<div class="auth-links">
<p>Don't have an account? <a href="/register">Register here</a></p>
<p><a href="/password/reset">Forgot password?</a></p>
</div>
</div>
</div>
+13
View File
@@ -0,0 +1,13 @@
# frozen_string_literal: true
module EuchreCamp
module Views
module Auth
module Login
class Create < EuchreCamp::View
expose :current_player
end
end
end
end
end
+13
View File
@@ -0,0 +1,13 @@
# frozen_string_literal: true
module EuchreCamp
module Views
module Auth
module Login
class Show < EuchreCamp::View
expose :current_player
end
end
end
end
end