diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 65da307..d2f42c7 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -4,12 +4,24 @@ class ApplicationController < ActionController::Base # Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has. allow_browser versions: :modern - include SessionsHelper - before_action :check_logged_in + before_action :authenticate + helper_method :logged_in?, :current_user - def check_logged_in - return if current_user + private - redirect_to root_path + def logged_in? + !!current_user + end + + def current_user + return unless session[:user_id] + + @current_user ||= User.find(session[:user_id]) + end + + def authenticate + return if logged_in? + + redirect_to root_path, alert: 'ログインしてください' end end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 0d073f6..5dc71da 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class HomeController < ApplicationController - skip_before_action :check_logged_in, only: %i[index welcome terms privacy] + skip_before_action :authenticate, only: %i[index welcome terms privacy] def index redirect_to meal_plans_path if current_user.present? diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index e8254db..84cb88b 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,13 +1,13 @@ # frozen_string_literal: true class SessionsController < ApplicationController - skip_before_action :check_logged_in, only: :create + skip_before_action :authenticate, only: :create def create user = User.find_or_new_from_auth_hash(request.env['omniauth.auth']) user.find_or_new_family(request.env['omniauth.params']['invitation_token']) if user.family_id.nil? if user.save - log_in user + session[:user_id] = user.id redirect_to meal_plans_path, notice: 'ログインに成功しました' else redirect_to root_path, notice: 'ログインに失敗しました' @@ -15,7 +15,7 @@ def create end def destroy - log_out + reset_session redirect_to root_path, notice: 'ログアウトしました' end end diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb index 6917975..97aeb4c 100644 --- a/app/helpers/sessions_helper.rb +++ b/app/helpers/sessions_helper.rb @@ -1,18 +1,4 @@ # frozen_string_literal: true module SessionsHelper - def current_user - return unless (user_id = session[:user_id]) - - @current_user ||= User.find_by(id: user_id) - end - - def log_in(user) - session[:user_id] = user.id - end - - def log_out - session.delete(:user_id) - remove_instance_variable(:@current_user) - end end diff --git a/app/views/shared/_header.html.erb b/app/views/shared/_header.html.erb index 1c6d9be..496b6e4 100644 --- a/app/views/shared/_header.html.erb +++ b/app/views/shared/_header.html.erb @@ -3,7 +3,7 @@ <%= link_to image_tag('logo_small.png', class: 'h-11'), root_path %>
- <% if current_user %> + <% if logged_in? %> <%= link_to image_tag(current_user.icon, class: 'icon mx-2'), user_path %> <%= render 'shared/menu' %> <% end %>