Skip to content

Commit

Permalink
feat: ログアウト処理を追加 (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
djkazunoko committed Apr 10, 2024
1 parent c83c9bf commit aede010
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 3 deletions.
7 changes: 7 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions app/controllers/user_sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 6 additions & 3 deletions app/views/groups/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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次会グループ一覧
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions spec/requests/user_sessions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 17 additions & 0 deletions spec/system/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit aede010

Please sign in to comment.