refactor: Restructure authentication into separate actions
This commit is contained in:
@@ -1,63 +0,0 @@
|
|||||||
# 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
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
<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>
|
|
||||||
Reference in New Issue
Block a user