From 42a814c658e3f45bdfe84bb4735fd52efc631b94 Mon Sep 17 00:00:00 2001 From: kinoppyd Date: Thu, 28 Mar 2024 01:25:07 +0900 Subject: [PATCH] Implements signup with GitHub --- app/controllers/application_controller.rb | 10 ++++ app/controllers/profiles_controller.rb | 7 +++ app/controllers/sessions_controller.rb | 24 ++++++++- app/helpers/profiles_helper.rb | 4 ++ app/views/layouts/_header.html.erb | 18 ++++++- app/views/profiles/show.html.erb | 37 +++++++++++++ config/locales/en.yml | 2 + config/locales/ja.yml | 2 + config/routes.rb | 5 +- public/icons/github-mark.svg | 1 + test/controllers/profiles_controller_test.rb | 9 ++++ test/controllers/sessions_controller_test.rb | 55 ++++++++++++++++++-- 12 files changed, 166 insertions(+), 8 deletions(-) create mode 100644 app/controllers/profiles_controller.rb create mode 100644 app/helpers/profiles_helper.rb create mode 100644 app/views/profiles/show.html.erb create mode 100644 public/icons/github-mark.svg create mode 100644 test/controllers/profiles_controller_test.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d5b6a577..554c594b 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 @@ -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 diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb new file mode 100644 index 00000000..8d9b3511 --- /dev/null +++ b/app/controllers/profiles_controller.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class ProfilesController < ApplicationController + include EventRouting + + def show; end +end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 98578400..b1812264 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -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 diff --git a/app/helpers/profiles_helper.rb b/app/helpers/profiles_helper.rb new file mode 100644 index 00000000..9ba602e9 --- /dev/null +++ b/app/helpers/profiles_helper.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +module ProfilesHelper +end diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 43bc2363..c67c020d 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -1,6 +1,6 @@