-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
166 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class ProfilesController < ApplicationController | ||
include EventRouting | ||
|
||
def show; end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
class SessionsController < ApplicationController | ||
skip_before_action :set_user | ||
skip_before_action :set_plan | ||
skip_before_action :set_locale | ||
skip_before_action :set_last_path | ||
|
||
def create | ||
user_info = request.env['omniauth.auth'] | ||
redirect_to session[:last_path] | ||
profile = Profile.find_by(uid: user_info['uid']) | ||
|
||
if profile | ||
session[:user_id] = profile.user.id | ||
else | ||
create_and_set_user unless @user | ||
@user.create_profile( | ||
provider: user_info['provider'], | ||
uid: user_info['uid'], | ||
name: user_info['info']['name'], | ||
email: user_info['info']['email'], | ||
avatar_url: user_info['info']['image'] | ||
) | ||
end | ||
|
||
redirect_to session[:last_path] || root_path | ||
end | ||
|
||
def delete | ||
session[:user_id] = nil | ||
|
||
redirect_to root_path | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
module ProfilesHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<% if @user && @user.profile %> | ||
<div class="flex justify-center"> | ||
<div class="w-1/2"> | ||
<div class="w-full min-w-[400px] h-fit p-4 shadow-[0_1px_2px_0_rgba(3,3,2,0.3)] rounded-md bg-white"> | ||
<div class="flex gap-4"> | ||
<img class="w-36 h-36 rounded-full" src="<%= @user.profile.avatar_url %>"> | ||
<div class="flex flex-col gap-2 w-full"> | ||
<div class="flex-grow flex flex-col gap-2"> | ||
<h1 class="text-4xl"><%= @user.profile.name %></h1> | ||
<h2><%= @user.profile.email %></h2> | ||
</div> | ||
<div class="self-end"> | ||
<%= form_with(url: '/session', method: :delete) do |f| %> | ||
<%= f.submit I18n.t('button.signout'), class: "p-2 text-sm m-h-[calc(0.857143rem+18px)] normal-button" %> | ||
<% end %> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<% else %> | ||
<div class="flex items-center justify-center h-96"> | ||
<div class="h-auto"> | ||
<%= form_tag('/auth/github', method: 'post' , data: {turbo: false}) do %> | ||
<button type='submit' class="p-2 border border-black border-solid rounded-md"> | ||
<div class="flex items-center gap-2"> | ||
<img src="/icons/github-mark.svg" class="w-6 h-6"> | ||
<span class="font-bold"> | ||
Sign up with GitHub | ||
</span> | ||
</div> | ||
</button> | ||
<% end %> | ||
</div> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class ProfilesControllerTest < ActionDispatch::IntegrationTest | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,56 @@ | |
require 'test_helper' | ||
|
||
class SessionsControllerTest < ActionDispatch::IntegrationTest | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
OmniAuth.config.test_mode = true | ||
|
||
def callback_uid(uid) | ||
OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new( | ||
{ | ||
provider: 'github', | ||
uid:, | ||
info: { | ||
name: 'test person', | ||
email: '[email protected]', | ||
image: 'https://example.com/avatar' | ||
} | ||
} | ||
) | ||
end | ||
|
||
test 'create new profile when user sign up' do | ||
callback_uid('12345678') | ||
assert_changes -> { Profile.count } do | ||
get '/auth/github/callback' | ||
end | ||
end | ||
|
||
test "don't create new profile when user already signed up" do | ||
callback_uid('1234') # fixture user | ||
assert_no_changes -> { Profile.count } do | ||
get '/auth/github/callback' | ||
end | ||
end | ||
|
||
test 'inherit plan if user created plan before sign up' do | ||
callback_uid('12345678') | ||
post event_plans_path(event_name: events(:kaigi).name), params: { | ||
plan: { | ||
title: 'plan for kaigi', | ||
initial: true, | ||
add_schedule_id: schedules(:kaigi_day1_time1_track1).id | ||
} | ||
} | ||
|
||
assert_no_changes -> { User.find(session[:user_id]).plans.where(event: events(:kaigi)).recent&.first } do | ||
get '/auth/github/callback' | ||
end | ||
end | ||
|
||
test 'remove session' do | ||
callback_uid('1234') # fixture user | ||
get '/auth/github/callback' | ||
assert_equal session[:user_id], users(:one).id | ||
delete '/session' | ||
assert_nil session[:user_id] | ||
end | ||
end |