Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ログイン状態を管理するメソッドを修正 #121

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -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?
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# 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: 'ログインに失敗しました'
end
end

def destroy
log_out
reset_session
redirect_to root_path, notice: 'ログアウトしました'
end
end
14 changes: 0 additions & 14 deletions app/helpers/sessions_helper.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/views/shared/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<%= link_to image_tag('logo_small.png', class: 'h-11'), root_path %>
</div>
<div class="flex justify-between items-center">
<% if current_user %>
<% if logged_in? %>
<%= link_to image_tag(current_user.icon, class: 'icon mx-2'), user_path %>
<%= render 'shared/menu' %>
<% end %>
Expand Down