From 76a46f51ed27ca9a0ce8d73db5967a74a2489c1b Mon Sep 17 00:00:00 2001 From: David Gwilliam Date: Fri, 27 Mar 2026 13:09:46 -0700 Subject: [PATCH] feat: Add authentication templates and actions --- app/actions/auth/login/create.rb | 52 ++++++++++++++++++++++++++ app/actions/auth/login/show.rb | 27 +++++++++++++ app/templates/auth/login/show.html.erb | 39 +++++++++++++++++++ app/views/auth/login/create.rb | 13 +++++++ app/views/auth/login/show.rb | 13 +++++++ 5 files changed, 144 insertions(+) create mode 100644 app/actions/auth/login/create.rb create mode 100644 app/actions/auth/login/show.rb create mode 100644 app/templates/auth/login/show.html.erb create mode 100644 app/views/auth/login/create.rb create mode 100644 app/views/auth/login/show.rb diff --git a/app/actions/auth/login/create.rb b/app/actions/auth/login/create.rb new file mode 100644 index 0000000..1b9a87b --- /dev/null +++ b/app/actions/auth/login/create.rb @@ -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 diff --git a/app/actions/auth/login/show.rb b/app/actions/auth/login/show.rb new file mode 100644 index 0000000..d76aea5 --- /dev/null +++ b/app/actions/auth/login/show.rb @@ -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 diff --git a/app/templates/auth/login/show.html.erb b/app/templates/auth/login/show.html.erb new file mode 100644 index 0000000..c38d99c --- /dev/null +++ b/app/templates/auth/login/show.html.erb @@ -0,0 +1,39 @@ +
+
+

Login

+ + <% if flash[:error] %> +
<%= flash[:error] %>
+ <% end %> + + <% if flash[:success] %> +
<%= flash[:success] %>
+ <% end %> + +
+
+ + +
+ +
+ + +
+ +
+ +
+ + +
+ + +
+
diff --git a/app/views/auth/login/create.rb b/app/views/auth/login/create.rb new file mode 100644 index 0000000..9cc3e95 --- /dev/null +++ b/app/views/auth/login/create.rb @@ -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 diff --git a/app/views/auth/login/show.rb b/app/views/auth/login/show.rb new file mode 100644 index 0000000..e32992a --- /dev/null +++ b/app/views/auth/login/show.rb @@ -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