From 557bce59c224c9441d6215b87f8a4335ee558aa9 Mon Sep 17 00:00:00 2001 From: juls Date: Sun, 20 Oct 2019 19:18:46 +0800 Subject: [PATCH 01/22] adds omniauth github as dependency to Gemfiles --- Gemfile | 1 + Gemfile.lock | 4 ++++ 2 files changed, 5 insertions(+) mode change 100644 => 100755 Gemfile.lock diff --git a/Gemfile b/Gemfile index 9f3d3046cd..1371390536 100644 --- a/Gemfile +++ b/Gemfile @@ -30,6 +30,7 @@ gem 'cloudinary', '~> 1.11.1' gem 'figaro', '1.1.1' gem 'google-api-client', '~> 0.30.2' gem 'kaminari', '1.1.1' +gem 'omniauth-github' gem 'omniauth-google-oauth2', '~> 0.7.0' gem 'omniauth-rails_csrf_protection', '~> 0.1.2' gem 'premailer-rails' diff --git a/Gemfile.lock b/Gemfile.lock old mode 100644 new mode 100755 index db9f4846c1..d948359015 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -225,6 +225,9 @@ GEM omniauth (1.9.1) hashie (>= 3.4.6) rack (>= 1.6.2, < 3) + omniauth-github (1.3.0) + omniauth (~> 1.5) + omniauth-oauth2 (>= 1.4.0, < 2.0) omniauth-google-oauth2 (0.7.0) jwt (>= 2.0) omniauth (>= 1.1.1) @@ -522,6 +525,7 @@ DEPENDENCIES jquery-rails (= 4.3.5) kaminari (= 1.1.1) letter_opener + omniauth-github omniauth-google-oauth2 (~> 0.7.0) omniauth-rails_csrf_protection (~> 0.1.2) pg (= 1.1.4) From 4d66809938e5e0424d3338ff95a11f98a7d69865 Mon Sep 17 00:00:00 2001 From: juls Date: Sun, 20 Oct 2019 19:27:48 +0800 Subject: [PATCH 02/22] add locales --- config/locales/en.yml | 2 ++ config/locales/sv.yml | 2 ++ 2 files changed, 4 insertions(+) diff --git a/config/locales/en.yml b/config/locales/en.yml index 9c7d2e3ffe..ed621520b0 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1107,6 +1107,8 @@ en: shared: sign_in_google: 'Sign in with Google' sign_up_google: 'Sign up with Google' + sign_in_github: 'Sign in with Github' + sign_up_github: 'Sign up with Github' confirmation: 'Didn''t receive confirmation instructions?' unlock: 'Didn''t receive unlock instructions?' unlock: diff --git a/config/locales/sv.yml b/config/locales/sv.yml index c91b9b43ab..a0a796ec9a 100644 --- a/config/locales/sv.yml +++ b/config/locales/sv.yml @@ -952,6 +952,8 @@ sv: shared: sign_in_google: 'Logga in med Google' sign_up_google: 'Registrera dig hos Google' + sign_in_github: 'Logga in med Github' + sign_up_github: 'Registrera dig hos Github' confirmation: 'Fick du inte bekräftelsesinstruktioner?' unlock: 'Fick du inte upplåsningsinstruktioner?' unlock: From d1771fb11d46448ee40cfba3204e4b679ac85bfd Mon Sep 17 00:00:00 2001 From: juls Date: Sun, 20 Oct 2019 19:28:30 +0800 Subject: [PATCH 03/22] adds omniauth on devise initializer --- config/initializers/devise.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index a29cf9033c..96958113e9 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -317,4 +317,6 @@ approval_prompt: 'select_account consent force', scope: 'userinfo.email,userinfo.profile,calendar' ) + + config.omniauth :github, ENV['GITHUB_CLIENT_ID'], ENV['GITHUB_CLIENT_SECRET'], scope: 'user:email' end From 06f2740b8340b22bcd224afc88f346f90228d41d Mon Sep 17 00:00:00 2001 From: juls Date: Sun, 20 Oct 2019 19:33:31 +0800 Subject: [PATCH 04/22] adds github oauth2 as omniauth providers --- app/models/user.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index 01cd481739..bb520f8a6e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -55,7 +55,7 @@ class User < ApplicationRecord # :confirmable, :lockable, :timeoutable and :omniauthable devise :invitable, :database_authenticatable, :registerable, :uid, :recoverable, :rememberable, :trackable, :validatable, :omniauthable, - omniauth_providers: [:google_oauth2] + omniauth_providers: [:google_oauth2, :github_oauth2] mount_uploader :avatar, AvatarUploader From 6b1698aa555c93a52f8217bc4096aab1c7ae69b5 Mon Sep 17 00:00:00 2001 From: juls Date: Sun, 20 Oct 2019 20:55:46 +0800 Subject: [PATCH 05/22] adds github method to persist user - should be refactored to be reusable across different providers --- app/models/user.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index bb520f8a6e..85da48b6a0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -55,7 +55,7 @@ class User < ApplicationRecord # :confirmable, :lockable, :timeoutable and :omniauthable devise :invitable, :database_authenticatable, :registerable, :uid, :recoverable, :rememberable, :trackable, :validatable, :omniauthable, - omniauth_providers: [:google_oauth2, :github_oauth2] + omniauth_providers: [:google_oauth2, :github] mount_uploader :avatar, AvatarUploader @@ -95,6 +95,17 @@ def self.find_for_google_oauth2(access_token) user end + # to refactor, could be single oauth method to begin with + def self.from_omniauth(auth) + where(provider: auth.provider, uid: auth.uid).first_or_create do |user| + user.provider = auth.provider + user.name = auth.info.name + user.uid = auth.uid + user.email = auth.info.email + user.password = Devise.friendly_token[0, 20] + end + end + def google_access_token google_access_token_expired? ? update_access_token : token end From 6e6a3ac6772a03c1a79882d21b783b2ad31a9950 Mon Sep 17 00:00:00 2001 From: juls Date: Sun, 20 Oct 2019 20:56:40 +0800 Subject: [PATCH 06/22] initial controller action for github omniauth --- app/controllers/omniauth_callbacks_controller.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/controllers/omniauth_callbacks_controller.rb b/app/controllers/omniauth_callbacks_controller.rb index 7805994619..e11966c1de 100644 --- a/app/controllers/omniauth_callbacks_controller.rb +++ b/app/controllers/omniauth_callbacks_controller.rb @@ -14,6 +14,19 @@ def google_oauth2 end end + def github + @user = User.from_omniauth(request.env["omniauth.auth"]) + + if @user.valid? + flash[:notice] = I18n.t('devise.omniauth_callbacks.success', + kind: t('omniauth.google')) + + sign_in_and_redirect @user, event: :authentication + else + redirect_to new_user_session_path, notice: t('omniauth.access_denied') + end + end + private def user From 67c8e5e2559d432cb3c94f2c2a1ad5915a7054f0 Mon Sep 17 00:00:00 2001 From: juls Date: Sun, 20 Oct 2019 21:35:13 +0800 Subject: [PATCH 07/22] updates github user builder - mvp version - skips validation of password when user is created via oauth --- app/models/user.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 85da48b6a0..f13d4acb32 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -81,7 +81,9 @@ class User < ApplicationRecord validates :locale, inclusion: { in: Rails.application.config.i18n.available_locales.map(&:to_s).push(nil) } - validate :password_complexity + + # add unless statement here instead of the concern, to make it more readable and composable + validate :password_complexity, unless: :oauth_provided? def active_for_authentication? super && !banned @@ -102,7 +104,7 @@ def self.from_omniauth(auth) user.name = auth.info.name user.uid = auth.uid user.email = auth.info.email - user.password = Devise.friendly_token[0, 20] + user.password ||= Devise.friendly_token[0, 20] end end @@ -110,6 +112,7 @@ def google_access_token google_access_token_expired? ? update_access_token : token end + # TODO: refactor this and use one checker def google_oauth2_enabled? token.present? end @@ -151,6 +154,11 @@ def update_access_token private + # checks if user is created from oauth + def oauth_provided? + provider.present? || token.present? + end + def google_access_token_expired? !access_expires_at || Time.zone.now > access_expires_at end From bc9eb4c36ac103ff64260208ccc2057502924088 Mon Sep 17 00:00:00 2001 From: juls Date: Sun, 20 Oct 2019 21:35:56 +0800 Subject: [PATCH 08/22] adds locale for github --- app/controllers/omniauth_callbacks_controller.rb | 2 +- config/locales/en.yml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/omniauth_callbacks_controller.rb b/app/controllers/omniauth_callbacks_controller.rb index e11966c1de..c311080b8a 100644 --- a/app/controllers/omniauth_callbacks_controller.rb +++ b/app/controllers/omniauth_callbacks_controller.rb @@ -19,7 +19,7 @@ def github if @user.valid? flash[:notice] = I18n.t('devise.omniauth_callbacks.success', - kind: t('omniauth.google')) + kind: t('omniauth.github')) sign_in_and_redirect @user, event: :authentication else diff --git a/config/locales/en.yml b/config/locales/en.yml index ed621520b0..7e4ed96c59 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -483,6 +483,7 @@ en: %{code_of_conduct_link}. You are no longer banned. omniauth: google: Google + github: Github access_denied: 'Access Denied' pages: about: From e94f239da3240d29a377c4199a42b766d7f18ec2 Mon Sep 17 00:00:00 2001 From: juls Date: Sun, 20 Oct 2019 21:36:13 +0800 Subject: [PATCH 09/22] adds links for github - to be refactored - working version --- app/views/devise/shared/_links.erb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/app/views/devise/shared/_links.erb b/app/views/devise/shared/_links.erb index 6a446c8d79..53000696ec 100644 --- a/app/views/devise/shared/_links.erb +++ b/app/views/devise/shared/_links.erb @@ -1,10 +1,15 @@
<%- if devise_mapping.omniauthable? and request.base_url != 'http://0.0.0.0:3000' %> - <%- resource_class.omniauth_providers.each do |provider| %> - <% if provider.to_s == "google_oauth2" && (current_page?(new_user_session_path) || current_page?(new_user_password_path)) %> - <%= button_to t('devise.shared.sign_in_google'), omniauth_authorize_path(resource_name, provider), method: :post, class: 'buttonGhostM' %> - <% elsif provider.to_s == "google_oauth2" %> - <%= button_to t('devise.shared.sign_up_google'), omniauth_authorize_path(resource_name, provider), method: :post, class: 'buttonGhostM' %> + <%- resource_class.omniauth_providers.each do |provider| %> + <% if provider.to_s == "google_oauth2" && (current_page?(new_user_session_path) || current_page?(new_user_password_path)) %> + <%= button_to t('devise.shared.sign_in_google'), omniauth_authorize_path(resource_name, provider), method: :post, class: 'buttonGhostM' %> + <% elsif provider.to_s == "github" && (current_page?(new_user_session_path) || current_page?(new_user_password_path)) %> + <%= button_to t('devise.shared.sign_in_github'), omniauth_authorize_path(resource_name, provider), method: :post, class: 'buttonGhostM' %> + <% elsif provider.to_s == "google_oauth2" %> + <%= button_to t('devise.shared.sign_up_google'), omniauth_authorize_path(resource_name, provider), method: :post, class: 'buttonGhostM' %> + <% elsif provider.to_s == 'github' %> + <%= button_to t('devise.shared.sign_up_github'), omniauth_authorize_path(resource_name, provider), method: :post, class: 'buttonGhostM' %> + <% end -%> <% end -%> <% end -%> <% end -%> From 69c7cf4084786b405254e8cfa3f9d2f7db92bd8a Mon Sep 17 00:00:00 2001 From: juls Date: Sun, 20 Oct 2019 21:37:04 +0800 Subject: [PATCH 10/22] adds ap gem for dev --- Gemfile | 1 + Gemfile.lock | 2 ++ 2 files changed, 3 insertions(+) diff --git a/Gemfile b/Gemfile index 1371390536..190701795a 100644 --- a/Gemfile +++ b/Gemfile @@ -86,6 +86,7 @@ group :development, :test do gem 'bullet' gem 'webdrivers' + gem 'awesome_print' end group :test do diff --git a/Gemfile.lock b/Gemfile.lock index d948359015..9b94801e5a 100755 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -51,6 +51,7 @@ GEM rake (>= 10.4, < 13.0) arel (9.0.0) ast (2.4.0) + awesome_print (1.8.0) aws_cf_signer (0.1.3) bcrypt (3.1.13) better_errors (2.6.0) @@ -498,6 +499,7 @@ PLATFORMS DEPENDENCIES activerecord-import annotate (~> 2.7) + awesome_print bcrypt (= 3.1.13) better_errors (~> 2.5) bullet From e4d6359c32d77fb332e9963b68b1acaa0b5333b1 Mon Sep 17 00:00:00 2001 From: juls Date: Fri, 25 Oct 2019 19:44:08 +0800 Subject: [PATCH 11/22] updates auth on github to save uid unique - uid uses "provider" + uid --- app/controllers/omniauth_callbacks_controller.rb | 2 +- app/models/user.rb | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/controllers/omniauth_callbacks_controller.rb b/app/controllers/omniauth_callbacks_controller.rb index c311080b8a..4be67e209d 100644 --- a/app/controllers/omniauth_callbacks_controller.rb +++ b/app/controllers/omniauth_callbacks_controller.rb @@ -15,7 +15,7 @@ def google_oauth2 end def github - @user = User.from_omniauth(request.env["omniauth.auth"]) + @user = User.from_omniauth request.env["omniauth.auth"] if @user.valid? flash[:notice] = I18n.t('devise.omniauth_callbacks.success', diff --git a/app/models/user.rb b/app/models/user.rb index f13d4acb32..da7937889d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -89,11 +89,11 @@ def active_for_authentication? super && !banned end - def self.find_for_google_oauth2(access_token) - user = find_or_initialize_by(email: access_token.info.email) - user.name ||= access_token.info.name + def self.find_for_google_oauth2(auth) + user = find_or_initialize_by(email: auth.info.email) + user.name ||= auth.info.name user.password ||= Devise.friendly_token[0, 20] - update_access_token_fields(user: user, access_token: access_token) + update_access_token_fields(user: user, access_token: auth) user end @@ -102,9 +102,9 @@ def self.from_omniauth(auth) where(provider: auth.provider, uid: auth.uid).first_or_create do |user| user.provider = auth.provider user.name = auth.info.name - user.uid = auth.uid + user.uid = auth.provider + auth.uid user.email = auth.info.email - user.password ||= Devise.friendly_token[0, 20] + user.password = Devise.friendly_token[0, 20] end end From 55fbb031ad894740130c87b460f8be1e19ce209e Mon Sep 17 00:00:00 2001 From: juls Date: Sun, 27 Oct 2019 22:20:01 +0800 Subject: [PATCH 12/22] uses alert option instead of notice to display authentication errors --- app/controllers/omniauth_callbacks_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/omniauth_callbacks_controller.rb b/app/controllers/omniauth_callbacks_controller.rb index 4be67e209d..1984f650f1 100644 --- a/app/controllers/omniauth_callbacks_controller.rb +++ b/app/controllers/omniauth_callbacks_controller.rb @@ -10,7 +10,7 @@ def google_oauth2 kind: t('omniauth.google')) sign_in_and_redirect @user, event: :authentication else - redirect_to new_user_session_path, notice: t('omniauth.access_denied') + redirect_to new_user_session_path, alert: t('omniauth.access_denied') end end @@ -23,7 +23,7 @@ def github sign_in_and_redirect @user, event: :authentication else - redirect_to new_user_session_path, notice: t('omniauth.access_denied') + redirect_to new_user_session_path, alert: t('omniauth.access_denied') end end From 25f05a51246f3e92778f5dc7be374cb701ed82cd Mon Sep 17 00:00:00 2001 From: juls Date: Sun, 27 Oct 2019 22:20:32 +0800 Subject: [PATCH 13/22] adds new user builder --- app/services/user_builder/base.rb | 28 ++++++++++++++++++++++++++++ app/services/user_builder/builder.rb | 13 +++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 app/services/user_builder/base.rb create mode 100644 app/services/user_builder/builder.rb diff --git a/app/services/user_builder/base.rb b/app/services/user_builder/base.rb new file mode 100644 index 0000000000..23871f9004 --- /dev/null +++ b/app/services/user_builder/base.rb @@ -0,0 +1,28 @@ +module UserBuilder + class Base + + def self.call(user: user, auth: auth) + new(user: user, auth: auth) + end + + def initialize(user: user, auth: auth) + user.tap do + user.provider = auth.provider + user.name = auth.info.name + user.uid = provider_uid(auth) + user.email = auth.info.email + user.password = default_password + end + end + + private + + def default_password + Devise.friendly_token[0, 20] + end + + def provider_uid(auth) + auth.provider + auth.uid + end + end +end diff --git a/app/services/user_builder/builder.rb b/app/services/user_builder/builder.rb new file mode 100644 index 0000000000..f5c68158fa --- /dev/null +++ b/app/services/user_builder/builder.rb @@ -0,0 +1,13 @@ +module UserBuilder + class Builder + SERVICE = { + # 'google_oauth' => GoogleOauth2 + } + + # UserBuilder::Builder.build(user: user, provider: provider, auth: auth) + def self.build(user:, auth:) + service = SERVICE.fetch(auth.provider, UserBuilder::Base) + service.call(user: user, auth: auth) + end + end +end From 333028acebf839232d815e0ae76911cfd0a891a2 Mon Sep 17 00:00:00 2001 From: juls Date: Sun, 27 Oct 2019 22:27:17 +0800 Subject: [PATCH 14/22] adds comments to refactor google oauth 2 --- app/models/user.rb | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index da7937889d..02f443ffb5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -89,6 +89,8 @@ def active_for_authentication? super && !banned end + # TODO: to refactor and to move to builder pattern + # UserBuilder::GoogleOauth2 def self.find_for_google_oauth2(auth) user = find_or_initialize_by(email: auth.info.email) user.name ||= auth.info.name @@ -97,17 +99,6 @@ def self.find_for_google_oauth2(auth) user end - # to refactor, could be single oauth method to begin with - def self.from_omniauth(auth) - where(provider: auth.provider, uid: auth.uid).first_or_create do |user| - user.provider = auth.provider - user.name = auth.info.name - user.uid = auth.provider + auth.uid - user.email = auth.info.email - user.password = Devise.friendly_token[0, 20] - end - end - def google_access_token google_access_token_expired? ? update_access_token : token end @@ -162,4 +153,9 @@ def oauth_provided? def google_access_token_expired? !access_expires_at || Time.zone.now > access_expires_at end + + # this ensures that uids are unique across providers + def provider_uid(auth) + auth.provider + auth.uid + end end From dd6b7bb6c972deeacb1fe98e4b3592ebaab16de4 Mon Sep 17 00:00:00 2001 From: juls Date: Sun, 27 Oct 2019 22:29:05 +0800 Subject: [PATCH 15/22] adds margin bottom --- app/views/devise/shared/_links.erb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/views/devise/shared/_links.erb b/app/views/devise/shared/_links.erb index 53000696ec..a5ba3f18a6 100644 --- a/app/views/devise/shared/_links.erb +++ b/app/views/devise/shared/_links.erb @@ -1,15 +1,18 @@
<%- if devise_mapping.omniauthable? and request.base_url != 'http://0.0.0.0:3000' %> <%- resource_class.omniauth_providers.each do |provider| %> + <% if provider.to_s == "google_oauth2" && (current_page?(new_user_session_path) || current_page?(new_user_password_path)) %> - <%= button_to t('devise.shared.sign_in_google'), omniauth_authorize_path(resource_name, provider), method: :post, class: 'buttonGhostM' %> + <%= button_to t('devise.shared.sign_in_google'), omniauth_authorize_path(resource_name, provider), method: :post, class: 'buttonGhostM smallMarginBottom' %> <% elsif provider.to_s == "github" && (current_page?(new_user_session_path) || current_page?(new_user_password_path)) %> - <%= button_to t('devise.shared.sign_in_github'), omniauth_authorize_path(resource_name, provider), method: :post, class: 'buttonGhostM' %> + <%= button_to t('devise.shared.sign_in_github'), omniauth_authorize_path(resource_name, provider), method: :post, class: 'buttonGhostM smallMarginBottom' %> <% elsif provider.to_s == "google_oauth2" %> - <%= button_to t('devise.shared.sign_up_google'), omniauth_authorize_path(resource_name, provider), method: :post, class: 'buttonGhostM' %> + <%= button_to t('devise.shared.sign_up_google'), omniauth_authorize_path(resource_name, provider), method: :post, class: 'buttonGhostM smallMarginBottom' %> <% elsif provider.to_s == 'github' %> - <%= button_to t('devise.shared.sign_up_github'), omniauth_authorize_path(resource_name, provider), method: :post, class: 'buttonGhostM' %> + <%= button_to t('devise.shared.sign_up_github'), omniauth_authorize_path(resource_name, provider), method: :post, class: 'buttonGhostM smallMarginBottom' %> <% end -%> + + <% end -%> <% end -%> <% end -%> From 06b44820f117fe83b1ac6680cc63edcd9a7c3db3 Mon Sep 17 00:00:00 2001 From: juls Date: Sun, 27 Oct 2019 22:38:41 +0800 Subject: [PATCH 16/22] updates user builder --- app/models/user.rb | 12 +++++++----- app/services/user_builder/builder.rb | 1 + 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 02f443ffb5..b5261e55c0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -99,6 +99,13 @@ def self.find_for_google_oauth2(auth) user end + # to refactor, could be single oauth method to begin with + def self.from_omniauth(auth) + where(provider: auth.provider, uid: auth.provider + auth.uid).first_or_create do |user| + UserBuilder::Builder.build(user: user, auth: auth) + end + end + def google_access_token google_access_token_expired? ? update_access_token : token end @@ -153,9 +160,4 @@ def oauth_provided? def google_access_token_expired? !access_expires_at || Time.zone.now > access_expires_at end - - # this ensures that uids are unique across providers - def provider_uid(auth) - auth.provider + auth.uid - end end diff --git a/app/services/user_builder/builder.rb b/app/services/user_builder/builder.rb index f5c68158fa..3cf26626a9 100644 --- a/app/services/user_builder/builder.rb +++ b/app/services/user_builder/builder.rb @@ -1,5 +1,6 @@ module UserBuilder class Builder + # Adds new omni auth providers here SERVICE = { # 'google_oauth' => GoogleOauth2 } From 97641a7eed1e17efe2943fa259d55ac3fff66456 Mon Sep 17 00:00:00 2001 From: juls Date: Fri, 3 Apr 2020 00:40:33 +0800 Subject: [PATCH 17/22] adds missing keys for Omniauth Github - used english values for the meantime --- config/locales/de.yml | 3 +++ config/locales/es.yml | 3 +++ config/locales/fr.yml | 3 +++ config/locales/hi.yml | 3 +++ config/locales/it.yml | 3 +++ config/locales/nb.yml | 3 +++ config/locales/nl.yml | 3 +++ config/locales/pt-BR.yml | 3 +++ config/locales/sv.yml | 1 + config/locales/vi.yml | 3 +++ config/locales/zh-CN.yml | 3 +++ 11 files changed, 31 insertions(+) diff --git a/config/locales/de.yml b/config/locales/de.yml index 6ad91b5d80..6e73cf7b8e 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -511,6 +511,7 @@ de: mehr blockiert. omniauth: google: Google + github: Github access_denied: 'Zutritt verweigert' pages: about: @@ -1183,6 +1184,8 @@ de: shared: sign_in_google: 'Mit Google einloggen' sign_up_google: 'Mit Google anmelden' + sign_in_github: 'Sign in with Github' + sign_up_github: 'Sign up with Github' confirmation: 'Keine Anweisungen zur Bestätigung erhalten?' unlock: 'Keine Anweisungen zur Freischaltung erhalten?' unlock: diff --git a/config/locales/es.yml b/config/locales/es.yml index 068cceb515..71952f1f46 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -434,6 +434,7 @@ es: %{code_of_conduct_link}. Ya no estás baneado/a.' omniauth: google: Google + github: Github access_denied: 'Acceso Denegado' pages: about: @@ -972,6 +973,8 @@ es: shared: sign_in_google: 'Registrarse con Google' sign_up_google: 'Ingresar con Google' + sign_in_github: 'Sign in with Github' + sign_up_github: 'Sign up with Github' confirmation: '¿No recibiste instrucciones de confirmación?' unlock: '¿No recibiste instrucciones para desbloquear?' unlock: diff --git a/config/locales/fr.yml b/config/locales/fr.yml index b1269b46e4..e3ce1a0555 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -491,6 +491,7 @@ fr: %{code_of_conduct_link}. omniauth: google: Google + github: Github access_denied: 'Accès refusé' pages: about: @@ -1160,6 +1161,8 @@ fr: shared: sign_in_google: "S'identifier avec Google" sign_up_google: "S'enregistrer avec Google" + sign_in_github: 'Sign in with Github' + sign_up_github: 'Sign up with Github' confirmation: "Vous n'avez pas reçu vous informations de confirmation ?" unlock: >- Vous n'avez pas reçu vos informations pour débloquer votre compte ? diff --git a/config/locales/hi.yml b/config/locales/hi.yml index fb0bd6ae7a..0e52c37d10 100644 --- a/config/locales/hi.yml +++ b/config/locales/hi.yml @@ -414,6 +414,7 @@ hi: किया गया था। अब आप प्रतिबंधित नहीं हैं' omniauth: google: 'गूगल' + github: 'Github' access_denied: 'पहुंच अस्वीकृत' pages: about: @@ -888,6 +889,8 @@ hi: shared: sign_in_google: 'Google के साथ साइन इन करें' sign_up_google: 'Google के साथ साइन अप करें' + sign_in_github: 'Sign in with Github' + sign_up_github: 'Sign up with Github' confirmation: 'पुष्टि निर्देश प्राप्त नहीं हुआ?' unlock: 'क्या अनलॉक निर्देश प्राप्त नहीं होंगे?' unlock: diff --git a/config/locales/it.yml b/config/locales/it.yml index 0581085acc..1336501cd2 100644 --- a/config/locales/it.yml +++ b/config/locales/it.yml @@ -440,6 +440,7 @@ it: nostra %{code_of_conduct_link}. Non sei più bannato.' omniauth: google: Google + github: Github access_denied: 'Accesso Negato' pages: about: @@ -965,6 +966,8 @@ it: shared: sign_in_google: 'Effettua il login con Google' sign_up_google: 'Registrati con Google' + sign_in_github: 'Sign in with Github' + sign_up_github: 'Sign up with Github' confirmation: 'Non hai ricevuto le istruzioni per confermare la tua EMail?' unlock: 'Non hai ricevuto le istruzioni per sbloccare il tuo account?' unlock: diff --git a/config/locales/nb.yml b/config/locales/nb.yml index c950c522a8..e8c3b64061 100644 --- a/config/locales/nb.yml +++ b/config/locales/nb.yml @@ -436,6 +436,7 @@ nb: %{code_of_conduct_link}. Du er ikke lenger utestengt.' omniauth: google: Google + github: Github access_denied: 'Adgang avvist' pages: about: @@ -957,6 +958,8 @@ nb: shared: sign_in_google: 'Logg inn med Google' sign_up_google: 'Registrer deg med Google' + sign_in_github: 'Sign in with Github' + sign_up_github: 'Sign up with Github' confirmation: 'Mottok ikke bekreftelsesinstruksjoner?' unlock: 'Mottok ikke opplåsningsinstruksjoner?' unlock: diff --git a/config/locales/nl.yml b/config/locales/nl.yml index 274b5ce3d6..9b3d324dd1 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -447,6 +447,7 @@ nl: %{code_of_conduct_link} schond. Je bent niet langer verbannen.' omniauth: google: Google + github: Github access_denied: 'Toegang geweigerd' pages: about: @@ -976,6 +977,8 @@ nl: shared: sign_in_google: 'Log in met Google' sign_up_google: 'Registreren met Google' + sign_in_github: 'Sign in with Github' + sign_up_github: 'Sign up with Github' confirmation: 'Bevestiginginstructies niet ontvangen?' unlock: 'Ontgrendelingsinstructies niet ontvangen?' unlock: diff --git a/config/locales/pt-BR.yml b/config/locales/pt-BR.yml index a3c1247551..433bff4264 100644 --- a/config/locales/pt-BR.yml +++ b/config/locales/pt-BR.yml @@ -446,6 +446,7 @@ pt-BR: %{code_of_conduct_link}. Você não está mais banido.' omniauth: google: Google + github: Github access_denied: 'Acesso negado' pages: about: @@ -1000,6 +1001,8 @@ pt-BR: shared: sign_in_google: 'Entrar com sua conta Google' sign_up_google: 'Cadastre-se com sua conta Google' + sign_in_github: 'Sign in with Github' + sign_up_github: 'Sign up with Github' confirmation: 'Não recebeu as instruções de confirmação?' unlock: 'Não recebeu as instruções de desbloqueio?' unlock: diff --git a/config/locales/sv.yml b/config/locales/sv.yml index a0a796ec9a..e2c0d5dba2 100644 --- a/config/locales/sv.yml +++ b/config/locales/sv.yml @@ -433,6 +433,7 @@ sv: %{code_of_conduct_link}. Du är inte längre förbjuden.' omniauth: google: Google + github: Github access_denied: 'Åtkomst nekad' pages: about: diff --git a/config/locales/vi.yml b/config/locales/vi.yml index 54d8bab904..b50e4428d3 100644 --- a/config/locales/vi.yml +++ b/config/locales/vi.yml @@ -435,6 +435,7 @@ vi: %{code_of_conduct_link} của chúng tôi. Bạn không còn bị cấm nữa.' omniauth: google: Google + github: Github access_denied: 'Truy nhập từ chối' pages: about: @@ -965,6 +966,8 @@ vi: shared: sign_in_google: 'Đăng nhập với Google' sign_up_google: 'Đăng ký với Google' + sign_in_github: 'Sign in with Github' + sign_up_github: 'Sign up with Github' confirmation: 'Không nhận được hướng dẫn xác nhận?' unlock: 'Không nhận được hứng dẫn mở khóa?' unlock: diff --git a/config/locales/zh-CN.yml b/config/locales/zh-CN.yml index 80f38de8c5..5f076f7bc4 100644 --- a/config/locales/zh-CN.yml +++ b/config/locales/zh-CN.yml @@ -397,6 +397,7 @@ zh-CN: 您不再被禁止。' omniauth: google: 谷歌 + github: Github access_denied: '拒绝访问' pages: about: @@ -828,6 +829,8 @@ zh-CN: shared: sign_in_google: '使用Google登录' sign_up_google: '使用Google注册' + sign_in_github: 'Sign in with Github' + sign_up_github: 'Sign up with Github' confirmation: '没有收到确认指示吗?' unlock: '没有收到解锁指令吗?' unlock: From c82023f7d05f8d8e73c9db336e0456f298aae744 Mon Sep 17 00:00:00 2001 From: juls Date: Fri, 3 Apr 2020 00:46:44 +0800 Subject: [PATCH 18/22] Linter via rubocop --- Gemfile | 2 +- app/controllers/omniauth_callbacks_controller.rb | 2 +- app/models/user.rb | 2 +- app/services/user_builder/base.rb | 4 ++-- app/services/user_builder/builder.rb | 3 ++- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Gemfile b/Gemfile index 190701795a..ee53b9187b 100644 --- a/Gemfile +++ b/Gemfile @@ -85,8 +85,8 @@ group :development, :test do gem 'bullet' - gem 'webdrivers' gem 'awesome_print' + gem 'webdrivers' end group :test do diff --git a/app/controllers/omniauth_callbacks_controller.rb b/app/controllers/omniauth_callbacks_controller.rb index 1984f650f1..7e45c876bb 100644 --- a/app/controllers/omniauth_callbacks_controller.rb +++ b/app/controllers/omniauth_callbacks_controller.rb @@ -15,7 +15,7 @@ def google_oauth2 end def github - @user = User.from_omniauth request.env["omniauth.auth"] + @user = User.from_omniauth request.env['omniauth.auth'] if @user.valid? flash[:notice] = I18n.t('devise.omniauth_callbacks.success', diff --git a/app/models/user.rb b/app/models/user.rb index b5261e55c0..51326fa6ca 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -55,7 +55,7 @@ class User < ApplicationRecord # :confirmable, :lockable, :timeoutable and :omniauthable devise :invitable, :database_authenticatable, :registerable, :uid, :recoverable, :rememberable, :trackable, :validatable, :omniauthable, - omniauth_providers: [:google_oauth2, :github] + omniauth_providers: %i[google_oauth2 github] mount_uploader :avatar, AvatarUploader diff --git a/app/services/user_builder/base.rb b/app/services/user_builder/base.rb index 23871f9004..450612103c 100644 --- a/app/services/user_builder/base.rb +++ b/app/services/user_builder/base.rb @@ -1,6 +1,6 @@ +# frozen_string_literal: true module UserBuilder class Base - def self.call(user: user, auth: auth) new(user: user, auth: auth) end @@ -11,7 +11,7 @@ def initialize(user: user, auth: auth) user.name = auth.info.name user.uid = provider_uid(auth) user.email = auth.info.email - user.password = default_password + user.password = default_password end end diff --git a/app/services/user_builder/builder.rb b/app/services/user_builder/builder.rb index 3cf26626a9..c153302abb 100644 --- a/app/services/user_builder/builder.rb +++ b/app/services/user_builder/builder.rb @@ -1,9 +1,10 @@ +# frozen_string_literal: true module UserBuilder class Builder # Adds new omni auth providers here SERVICE = { # 'google_oauth' => GoogleOauth2 - } + }.freeze # UserBuilder::Builder.build(user: user, provider: provider, auth: auth) def self.build(user:, auth:) From b585f4966288b1c14d44a33d058af2e345b1f2fb Mon Sep 17 00:00:00 2001 From: juls Date: Fri, 3 Apr 2020 00:50:22 +0800 Subject: [PATCH 19/22] comments --- app/models/user.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index 51326fa6ca..266a0bf925 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -82,7 +82,8 @@ class User < ApplicationRecord in: Rails.application.config.i18n.available_locales.map(&:to_s).push(nil) } - # add unless statement here instead of the concern, to make it more readable and composable + # add unless statement here instead of the concern for readability + # TODO: refactor the Password validator concern validate :password_complexity, unless: :oauth_provided? def active_for_authentication? From c517f558d3e2f76a85a8fe94bb709572537a8d96 Mon Sep 17 00:00:00 2001 From: sbpipb Date: Fri, 3 Apr 2020 00:57:07 +0800 Subject: [PATCH 20/22] lint fixes --- app/models/user.rb | 3 ++- app/services/user_builder/base.rb | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 266a0bf925..f75bcf57e1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -102,7 +102,8 @@ def self.find_for_google_oauth2(auth) # to refactor, could be single oauth method to begin with def self.from_omniauth(auth) - where(provider: auth.provider, uid: auth.provider + auth.uid).first_or_create do |user| + where(provider: auth.provider, + uid: auth.provider + auth.uid).first_or_create do |user| UserBuilder::Builder.build(user: user, auth: auth) end end diff --git a/app/services/user_builder/base.rb b/app/services/user_builder/base.rb index 450612103c..a446b8e870 100644 --- a/app/services/user_builder/base.rb +++ b/app/services/user_builder/base.rb @@ -1,11 +1,11 @@ # frozen_string_literal: true module UserBuilder class Base - def self.call(user: user, auth: auth) + def self.call(user:, auth:) new(user: user, auth: auth) end - def initialize(user: user, auth: auth) + def initialize(user:, auth:) user.tap do user.provider = auth.provider user.name = auth.info.name From a1e85f3697af8076da7a8d0c68afe855551efde4 Mon Sep 17 00:00:00 2001 From: sbpipb Date: Mon, 6 Apr 2020 13:23:33 +0800 Subject: [PATCH 21/22] fix typo --- app/views/devise/shared/_links.erb | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/views/devise/shared/_links.erb b/app/views/devise/shared/_links.erb index a5ba3f18a6..8d0c78661e 100644 --- a/app/views/devise/shared/_links.erb +++ b/app/views/devise/shared/_links.erb @@ -11,9 +11,6 @@ <% elsif provider.to_s == 'github' %> <%= button_to t('devise.shared.sign_up_github'), omniauth_authorize_path(resource_name, provider), method: :post, class: 'buttonGhostM smallMarginBottom' %> <% end -%> - - - <% end -%> <% end -%> <% end -%>
From 411ac59adfeb07dca65695b9a97aa716839ae333 Mon Sep 17 00:00:00 2001 From: sbpipb Date: Wed, 8 Apr 2020 01:20:44 +0800 Subject: [PATCH 22/22] initial tests for user builder base --- app/services/user_builder/base.rb | 5 ++++- spec/services/user_builder/base.rb | 31 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 spec/services/user_builder/base.rb diff --git a/app/services/user_builder/base.rb b/app/services/user_builder/base.rb index a446b8e870..d0e03de1b0 100644 --- a/app/services/user_builder/base.rb +++ b/app/services/user_builder/base.rb @@ -1,12 +1,15 @@ # frozen_string_literal: true module UserBuilder class Base + attr_reader :user + def self.call(user:, auth:) new(user: user, auth: auth) end def initialize(user:, auth:) - user.tap do + @user = user + @user.tap do user.provider = auth.provider user.name = auth.info.name user.uid = provider_uid(auth) diff --git a/spec/services/user_builder/base.rb b/spec/services/user_builder/base.rb new file mode 100644 index 0000000000..67f413a4cd --- /dev/null +++ b/spec/services/user_builder/base.rb @@ -0,0 +1,31 @@ +describe UserBuilder::Base do + describe '.call' do + subject { described_class } + + let(:user_one) { FactoryBot.build(:user1) } + + let(:auth) { OmniAuth::AuthHash.new({ + :provider => 'github', + :uid => '123545', + :info => info }) + } + + let(:info) do + { name: 'joe', email: 'test@email.com' } + end + + let(:builder) { UserBuilder::Base.call(user: user_one, auth: auth) } + + it 'builds the user object' do + expect(builder.user).to eql(user_one) + end + + it 'creates correct user uid' do + expect(builder.user.uid).to eql('github123545') + end + + it 'creates user under provider' do + expect(builder.user.provider).to eql('github') + end + end +end