Check authentication on each request (#26)

Reviewed-on: #26
This commit is contained in:
Andrew Tomaka 2024-08-16 19:59:29 -04:00
parent 96ca8b7e7c
commit 452be0c49c
10 changed files with 59 additions and 10 deletions

View file

@ -1,2 +1,3 @@
class ApplicationController < ActionController::Base
include Authenticatable
end

View file

@ -0,0 +1,13 @@
module Authenticatable
extend ActiveSupport::Concern
included do
before_action :authenticate_user
end
private
def authenticate_user
Current.user = User.find_by(id: session[:current_user_id]) || GuestUser.new
end
end

3
app/models/current.rb Normal file
View file

@ -0,0 +1,3 @@
class Current < ActiveSupport::CurrentAttributes
attribute :user
end

3
app/models/guest_user.rb Normal file
View file

@ -0,0 +1,3 @@
class GuestUser
def registered? = false
end

View file

@ -1,3 +1,5 @@
class User < ApplicationRecord
has_secure_password
def registered? = true
end

View file

@ -19,7 +19,11 @@
<li class="mr-6"><%= link_to "Credit Card Bills", credit_card_bills_path, class: "text-white" %></li>
<li class="mr-6"><%= link_to "Incomes", incomes_path, class: "text-white" %></li>
<li class="mr-6"><%= link_to "Members", members_path, class: "text-white" %></li>
<li class="mr-6"><%= link_to "Log out", session_path, data: {turbo_method: :delete}, class: "text-white" %></li>
<% if Current.user.registered? %>
<li class="mr-6"><%= link_to "Log out", session_path, data: {turbo_method: :delete}, class: "text-white" %></li>
<% else %>
<li class="mr-6"><%= link_to "Log in", new_session_path, class: "text-white" %></li>
<% end %>
</ul>
</nav>