Skip to content

Commit

Permalink
feat: OAuth認証失敗時の処理を追加 (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
djkazunoko committed Apr 9, 2024
1 parent d01f307 commit 9312d58
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 24 deletions.
4 changes: 4 additions & 0 deletions app/controllers/user_sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ def destroy
reset_session
redirect_to root_path, notice: 'ログアウトしました'
end

def failure
redirect_to root_path, notice: 'ログインをキャンセルしました'
end
end
14 changes: 9 additions & 5 deletions config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Rails.application.config.middleware.use OmniAuth::Builder do
provider :github,
Rails.application.credentials.github[:client_id],
Rails.application.credentials.github[:client_secret]
end
Rails.application.config.middleware.use OmniAuth::Builder do
provider :github,
Rails.application.credentials.github[:client_id],
Rails.application.credentials.github[:client_secret]

OmniAuth.config.on_failure = Proc.new { |env|
OmniAuth::FailureEndpoint.new(env).redirect_to_failure
}
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
# Defines the root path route ("/")
root "groups#index"
get "auth/:provider/callback" => "user_sessions#create"
get "auth/failure" => "user_sessions#failure"
delete "/logout" => "user_sessions#destroy"
end
7 changes: 7 additions & 0 deletions spec/requests/user_sessions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,11 @@
expect(response).to redirect_to(root_path)
end
end

describe 'GET /failure' do
it 'redirects to root_path' do
get '/auth/failure'
expect(response).to redirect_to(root_path)
end
end
end
61 changes: 42 additions & 19 deletions spec/system/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,57 @@

RSpec.describe 'Users', type: :system do
describe 'user authentication' do
it 'allows users to login' do
visit root_path
expect(page).to have_content 'GitHubアカウントが必要です'
context 'when authentication is successful' do
it 'allows users to login' do
visit root_path
expect(page).to have_content 'GitHubアカウントが必要です'

expect do
click_button 'サインアップ / ログインをして2次会グループを作成'

expect(page).to have_content 'ログインしました'
end.to change(User, :count).by(1)

expect(page).to have_current_path(new_group_path)
end
end

context 'when user logs out' do
it 'allows users to logout' do
visit root_path
expect(page).to have_content 'GitHubアカウントが必要です'
expect(page).not_to have_content 'ログアウト'

expect do
click_button 'サインアップ / ログインをして2次会グループを作成'
expect(page).to have_current_path(new_group_path)

expect(page).to have_content 'ログインしました'
end.to change(User, :count).by(1)
click_link 'キャンセル'
expect(page).to have_current_path(groups_path)
expect(page).not_to have_content 'GitHubアカウントが必要です'

expect(page).to have_current_path(new_group_path)
click_button 'ログアウト'
expect(page).to have_content 'ログアウトしました'
expect(page).to have_content 'GitHubアカウントが必要です'
end
end

it 'allows users to logout' do
visit root_path
expect(page).to have_content 'GitHubアカウントが必要です'
expect(page).not_to have_content 'ログアウト'
context 'when authentication is failed' do
before do
OmniAuth.config.mock_auth[:github] = :invalid_credentials
end

it 'redirects to root_path' do
visit root_path
expect(page).to have_content 'GitHubアカウントが必要です'

click_button 'サインアップ / ログインをして2次会グループを作成'
expect(page).to have_current_path(new_group_path)
expect do
click_button 'サインアップ / ログインをして2次会グループを作成'

click_link 'キャンセル'
expect(page).to have_current_path(groups_path)
expect(page).not_to have_content 'GitHubアカウントが必要です'
expect(page).to have_content 'ログインをキャンセルしました'
end.not_to change(User, :count)

click_button 'ログアウト'
expect(page).to have_content 'ログアウトしました'
expect(page).to have_content 'GitHubアカウントが必要です'
expect(page).to have_current_path(root_path)
end
end
end
end

0 comments on commit 9312d58

Please sign in to comment.