diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7944f9f..1671234 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,4 +1,11 @@ # frozen_string_literal: true class ApplicationController < ActionController::Base + helper_method :logged_in? + + private + + def logged_in? + !!session[:user_id] + end end diff --git a/app/controllers/user_sessions_controller.rb b/app/controllers/user_sessions_controller.rb index 8d71605..816d593 100644 --- a/app/controllers/user_sessions_controller.rb +++ b/app/controllers/user_sessions_controller.rb @@ -6,4 +6,9 @@ def create session[:user_id] = user.id redirect_to new_group_path, notice: 'ログインしました' end + + def destroy + reset_session + redirect_to root_path, notice: 'ログアウトしました' + end end diff --git a/app/views/groups/index.html.slim b/app/views/groups/index.html.slim index 0cb071c..2f2d9e1 100644 --- a/app/views/groups/index.html.slim +++ b/app/views/groups/index.html.slim @@ -10,9 +10,12 @@ p.mb-4 = link_to '2次会グループを作成', new_group_path .mb-4 - = button_to 'サインアップ / ログインをして2次会グループを作成', '/auth/github', data: { turbo: false } - span.text-xs - | GitHubアカウントが必要です + - if logged_in? + = button_to 'ログアウト', logout_path, method: :delete + - else + = button_to 'サインアップ / ログインをして2次会グループを作成', '/auth/github', data: { turbo: false } + span.text-xs + | GitHubアカウントが必要です p.text-xl.font-bold.mb-4 | 2次会グループ一覧 diff --git a/config/routes.rb b/config/routes.rb index a3676cb..d14a46c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,4 +9,5 @@ # Defines the root path route ("/") root "groups#index" get "auth/:provider/callback" => "user_sessions#create" + delete "/logout" => "user_sessions#destroy" end diff --git a/spec/requests/user_sessions_spec.rb b/spec/requests/user_sessions_spec.rb index 9e3bce3..00ea616 100644 --- a/spec/requests/user_sessions_spec.rb +++ b/spec/requests/user_sessions_spec.rb @@ -48,4 +48,18 @@ end end end + + describe 'DELETE /destroy' do + it 'removes user id from session' do + get '/auth/github/callback' + expect(session[:user_id]).to be_present + delete '/logout' + expect(session[:user_id]).to be_nil + end + + it 'redirects to root_path' do + delete '/logout' + expect(response).to redirect_to(root_path) + end + end end diff --git a/spec/system/users_spec.rb b/spec/system/users_spec.rb index f25010d..d9c2212 100644 --- a/spec/system/users_spec.rb +++ b/spec/system/users_spec.rb @@ -20,5 +20,22 @@ expect(page).to have_current_path(new_group_path) end + + it 'allows users to logout' do + visit root_path + expect(page).to have_content 'GitHubアカウントが必要です' + expect(page).not_to have_content 'ログアウト' + + click_button 'サインアップ / ログインをして2次会グループを作成' + expect(page).to have_current_path(new_group_path) + + click_link 'キャンセル' + expect(page).to have_current_path(groups_path) + expect(page).not_to have_content 'GitHubアカウントが必要です' + + click_button 'ログアウト' + expect(page).to have_content 'ログアウトしました' + expect(page).to have_content 'GitHubアカウントが必要です' + end end end