From 8fe132f2d1ab67c39f9abf95a1ad6ead5b608545 Mon Sep 17 00:00:00 2001 From: Trevor Vallender Date: Sun, 21 Apr 2024 15:01:10 +0100 Subject: [PATCH] Sessions! --- .../account_verifications_controller.rb | 4 ++- app/controllers/application_controller.rb | 13 +++++++++ app/controllers/sessions_controller.rb | 28 +++++++++++++++++++ app/controllers/todos_controller.rb | 4 +++ app/controllers/users_controller.rb | 4 ++- app/helpers/sessions_helper.rb | 5 ++++ app/models/current.rb | 3 ++ app/views/layouts/application.html.erb | 9 ++++++ app/views/sessions/new.html.erb | 16 +++++++++++ app/views/todos/index.html.erb | 1 + app/views/users/new.html.erb | 4 +-- config/locales/en.yml | 13 +++++++++ config/routes.rb | 8 +++++- .../account_verifications_controller_test.rb | 2 +- test/controllers/users_controller_test.rb | 2 +- test/system/sessions_test.rb | 15 ++++++++++ test/system/sign_ups_test.rb | 2 +- 17 files changed, 125 insertions(+), 8 deletions(-) create mode 100644 app/controllers/sessions_controller.rb create mode 100644 app/controllers/todos_controller.rb create mode 100644 app/helpers/sessions_helper.rb create mode 100644 app/models/current.rb create mode 100644 app/views/sessions/new.html.erb create mode 100644 app/views/todos/index.html.erb create mode 100644 test/system/sessions_test.rb diff --git a/app/controllers/account_verifications_controller.rb b/app/controllers/account_verifications_controller.rb index 17c4fb0..31f4dc3 100644 --- a/app/controllers/account_verifications_controller.rb +++ b/app/controllers/account_verifications_controller.rb @@ -1,4 +1,6 @@ class AccountVerificationsController < ApplicationController + skip_before_action :authenticate, only: [ :show ] + def show user = User.find_by_token_for(:email_verification, params[:id]) unless user @@ -8,6 +10,6 @@ class AccountVerificationsController < ApplicationController user.update(verified: true) UserMailer.with(user: user).email_verified.deliver_later flash[:notice] = t(".success") - redirect_to :root # TODO: New session path + redirect_to login_path end end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 09705d1..faa1473 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,2 +1,15 @@ class ApplicationController < ActionController::Base + before_action :authenticate + + private + + def authenticate + Rails.logger.error "Session: #{session.inspect}" + if authenticated_user = User.find_by(id: session[:user_id]) + Current.user = authenticated_user + else + flash[:alert] = t("not_authenticated") + redirect_to login_path + end + end end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..a4f7b05 --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,28 @@ +class SessionsController < ApplicationController + skip_before_action :authenticate, only: [ :new, :create ] + + def new + end + + def create + Current.user = User.authenticate_by( + username: params[:username], + password: params[:password], + ) + if Current.user + session[:user_id] = Current.user.id + flash[:notice] = t(".success", name: Current.user.first_name) + redirect_to :root + else + flash[:alert] = t(".error") + render :new, status: :unprocessable_entity + end + end + + def destroy + reset_session + Current.user = nil + flash[:notice] = t(".success") + redirect_to login_path + end +end diff --git a/app/controllers/todos_controller.rb b/app/controllers/todos_controller.rb new file mode 100644 index 0000000..c0d227b --- /dev/null +++ b/app/controllers/todos_controller.rb @@ -0,0 +1,4 @@ +class TodosController < ApplicationController + def index + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index d8f90c3..901280d 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,4 +1,6 @@ class UsersController < ApplicationController + skip_before_action :authenticate, only: [ :new, :create ] + def new @user = User.new end @@ -9,7 +11,7 @@ class UsersController < ApplicationController token = @user.generate_token_for(:email_verification) UserMailer.with(user: @user, token: token).email_verification.deliver_later flash[:notice] = t(".success", name: @user.first_name) - redirect_to :root + redirect_to login_path else flash[:alert] = t(".error", error: @user.errors.full_messages.to_sentence) render :new, status: :unprocessable_entity diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb new file mode 100644 index 0000000..49ee652 --- /dev/null +++ b/app/helpers/sessions_helper.rb @@ -0,0 +1,5 @@ +module SessionsHelper + def logged_in? + session[:user_id].present? + end +end diff --git a/app/models/current.rb b/app/models/current.rb new file mode 100644 index 0000000..73a9744 --- /dev/null +++ b/app/models/current.rb @@ -0,0 +1,3 @@ +class Current < ActiveSupport::CurrentAttributes + attribute :user +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 11af577..a9767cd 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -16,6 +16,15 @@

<%= link_to t("forg"), root_path %>

+