Skip to content

Commit

Permalink
Implements signup with GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
kinoppyd committed Apr 2, 2024
1 parent b05d484 commit 42a814c
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 8 deletions.
10 changes: 10 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ def set_plan
end

def not_found(err)
print_error_if_test(err)
Rails.logger.debug("#{err}\n#{err.backtrace.join("\n")}")
render template: 'errors/not_found', status: 404, layout: 'application', content_type: 'text/html'
end

def server_error(err)
print_error_if_test(err)
Rails.logger.error("#{err}\n#{err.backtrace.join("\n")}")
render template: 'errors/server_error', status: 500, layout: 'application', content_type: 'text/html'
end
Expand Down Expand Up @@ -66,4 +68,12 @@ def with_time_zone(&)
yield
end
end

def print_error_if_test(err)
return unless Rails.env.test?

pp params
puts err.message
puts err.backtrace.join("\n")
end
end
7 changes: 7 additions & 0 deletions app/controllers/profiles_controller.rb
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
24 changes: 22 additions & 2 deletions app/controllers/sessions_controller.rb
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
4 changes: 4 additions & 0 deletions app/helpers/profiles_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

module ProfilesHelper
end
18 changes: 16 additions & 2 deletions app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<nav>
<div class="flex items-center direction-row h-[50px] bg-[var(--mainColor)]">
<div class="flex flex-row items-center mx-4 my-0 text-white">
<div class="flex items-center justify-end direction-row h-[50px] bg-[var(--mainColor)]">
<div class="flex flex-row flex-grow items-center mx-4 my-0 text-white">
<% unless Rails.env.test? %>
<img class="h-6" src="<%= asset_path("#{@event.name}/rubykaigi_logo.svg") %>">
<% end %>
Expand All @@ -20,6 +20,11 @@
</div>
</div>
</div>
<div class="flex items-center">
<% if @user&.profile %>
<img src="<%= @user.profile.avatar_url %>" class="rounded-full h-8 w-8 mr-4" >
<% end %>
</div>
</div>

<div data-controller="navigation" class="flex items-center min-w-max bg-white pr-6 pl-6 shadow-[rgba(3,3,2,0.3)_0_1px_2px_0] px-6">
Expand Down Expand Up @@ -64,6 +69,15 @@
<%= I18n.t('nav.plan') %></a>
</li>
<% end %>
<li>
<a
href="<%= event_profile_path %>"
class="flex items-center gap-2 h-max box-border m-0 boder-none bg-transparent px-2 py-3 <%= current_path?(event_profile_path) ? 'nav-current' : '' %>"
<%= sanitize current_path?(event_path) ? 'aria-current="page"' : '' %>
>
<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 512 512" width="1em" height="1em" class="" role="img" aria-hidden="true" focusable="false" xmlns="http://www.w3.org/2000/svg" style="color: rgb(112, 109, 101);"><path d="M256 288A144 144 0 1 0 256 0a144 144 0 1 0 0 288zm-94.7 32C72.2 320 0 392.2 0 481.3c0 17 13.8 30.7 30.7 30.7H481.3c17 0 30.7-13.8 30.7-30.7C512 392.2 439.8 320 350.7 320H161.3z"></path></svg>
<%= I18n.t('nav.profile') %></a>
</li>
</ul>
<div class="ml-auto">
<label class="flex items-center justify-start gap-2">
Expand Down
37 changes: 37 additions & 0 deletions app/views/profiles/show.html.erb
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 %>
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ en:
root: "Top"
schedule: "Schedule"
plan: "My Plans"
profile: "Profile"
info:
create_plan_title: "Create your RubyKaigi Plans"
create_plan_text: "Create your RubyKaigi Plans with RubyKaigi mie.ru kun. Agree to Terms of service and press create button."
Expand Down Expand Up @@ -94,6 +95,7 @@ en:
accept_to_add: "Accept and add to Plans"
make_editable: "Make editable"
check_password: "Take edit permission"
signout: "Sign out"
default_vales:
plan_title: "My plans"

Expand Down
2 changes: 2 additions & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ja:
root: "top"
schedule: "スケジュール"
plan: "視聴予定"
profile: "プロフィール"
info:
create_plan_title: "セッションの視聴予定を作成しませんか?"
create_plan_text: "RubyKaigi mie.ru 君を使って、RubyKaigi2021 Takeoutのセッション視聴予定表を作成しましょう! 利用規約に同意の上、予定の作成ボタンを押してください。"
Expand Down Expand Up @@ -63,6 +64,7 @@ ja:
accept_to_add: "同意して追加する"
make_editable: "パスワードを入力して編集"
check_password: "編集可能にする"
signout: "サインアウト"
default_vales:
plan_title: "視聴予定"

Expand Down
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
Rails.application.routes.draw do
root to: redirect('/2023')

get 'auth/:provider/callback', to: 'sessions#create'
get '/auth/:provider/callback', to: 'sessions#create'
delete '/session', to: 'sessions#delete'

scope '/:event_name', as: 'event' do
get '/', to: 'static#top'
Expand All @@ -19,6 +20,8 @@
resource :ogp, only: %i[show]
end
end

resource :profile, only: %i[show update]
end

get '*path', controller: 'application', action: 'not_found'
Expand Down
1 change: 1 addition & 0 deletions public/icons/github-mark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions test/controllers/profiles_controller_test.rb
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
55 changes: 52 additions & 3 deletions test/controllers/sessions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 42a814c

Please sign in to comment.