Skip to content

Commit 90b4e84

Browse files
Add support for google login (#317)
* Add support for google login * lint resolved * login with git and strava removed
1 parent 2e8fa03 commit 90b4e84

File tree

10 files changed

+34
-8
lines changed

10 files changed

+34
-8
lines changed

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Metrics/BlockLength:
6161
- "spec/**/*"
6262
- "config/environments/*"
6363
- "config/routes.rb"
64+
- "config/initializers/devise.rb"
6465

6566
Metrics/ClassLength:
6667
CountAsOne: ["array", "hash", "heredoc"]

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ gem "multi_json", "1.15.0"
4646
# gem "newrelic_rpm", "9.9.0"
4747
# GitHub strategy for OmniAuth
4848
gem "omniauth-github"
49+
# GitHub strategy for Google
50+
gem "omniauth-google-oauth2"
4951
# Provides CSRF protection on OmniAuth request endpoint on Rails application.
5052
gem "omniauth-rails_csrf_protection"
5153
# GitHub strategy for Strava

Gemfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@ GEM
316316
omniauth-github (2.0.1)
317317
omniauth (~> 2.0)
318318
omniauth-oauth2 (~> 1.8)
319+
omniauth-google-oauth2 (1.1.3)
320+
jwt (>= 2.0)
321+
oauth2 (~> 2.0)
322+
omniauth (~> 2.0)
323+
omniauth-oauth2 (~> 1.8)
319324
omniauth-oauth2 (1.8.0)
320325
oauth2 (>= 1.4, < 3)
321326
omniauth (~> 2.0)
@@ -616,6 +621,7 @@ DEPENDENCIES
616621
money (= 6.16.0)
617622
multi_json (= 1.15.0)
618623
omniauth-github
624+
omniauth-google-oauth2
619625
omniauth-rails_csrf_protection
620626
omniauth-strava
621627
packwerk (= 3.1.0)

app/models/user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class User < ApplicationRecord
88
devise :database_authenticatable, :registerable,
99
:recoverable, :rememberable, :validatable, :api,
1010
:omniauthable, :confirmable, :lockable,
11-
omniauth_providers: %i[github strava]
11+
omniauth_providers: %i[github strava google_oauth2]
1212

1313
has_many :event_procedures, dependent: :destroy
1414
has_many :medical_shifts, dependent: :destroy

app/views/devise/registrations/new.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</div>
2525

2626
<div class="submit">
27-
<%= f.submit "Sign up" %>
27+
<%= f.submit t("devise.registrations.new.sign_up") %>
2828
</div>
2929
<% end %>
3030

app/views/devise/sessions/new.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<% end %>
2222

2323
<div class="submit">
24-
<%= f.submit "Log in" %>
24+
<%= f.submit t("devise.sessions.new.sign_in") %>
2525
</div>
2626
<% end %>
2727

app/views/devise/shared/_links.html.erb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
</div>
2222

2323
<div class="oauth-buttons">
24-
<%- if devise_mapping.omniauthable? %>
25-
<%- resource_class.omniauth_providers.each do |provider| %>
26-
<p><%= button_to t("devise.shared.links.sign_in_with_provider"){OmniAuth::Utils.camelize(provider)}, omniauth_authorize_path(resource_name, provider), data: { turbo: false } %></p>
27-
<% end %>
24+
<%- if devise_mapping.omniauthable? && resource_class.omniauth_providers.include?(:google_oauth2) %>
25+
<p>
26+
<%= button_to t("devise.shared.links.sign_in_with_provider", provider: "Google"), omniauth_authorize_path(resource_name, :google_oauth2), data: { turbo: false } %>
27+
</p>
2828
<% end %>
2929
</div>

config/initializers/devise.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,16 @@
264264
config.omniauth :github, ENV.fetch("GITHUB_CLIENT_ID", nil), ENV.fetch("GITHUB_CLIENT_SECRET", nil),
265265
scope: "user,public_repo"
266266

267+
config.omniauth :google_oauth2,
268+
ENV.fetch("GOOGLE_CLIENT_ID", nil),
269+
ENV.fetch("GOOGLE_CLIENT_SECRET", nil),
270+
{
271+
scope: "userinfo.email,userinfo.profile",
272+
prompt: "select_account",
273+
image_aspect_ratio: "square",
274+
image_size: 50
275+
}
276+
267277
config.omniauth :strava, ENV.fetch("STRAVA_CLIENT_ID", nil).to_i, ENV.fetch("STRAVA_CLIENT_SECRET", nil),
268278
scope: "profile:read_all", token_params: {
269279
client_id: ENV.fetch("STRAVA_CLIENT_ID", nil),

packs/oauth/app/oauth/actors/find_or_create_user.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ module Actors
55
class FindOrCreateUser < Actor
66
ALLOWED_PROVIDERS_HASH = {
77
github: "github",
8-
strava: "strava"
8+
strava: "strava",
9+
google: "google_oauth2"
910
}.freeze
1011

1112
input :auth, type: OmniAuth::AuthHash
@@ -28,6 +29,8 @@ def find_or_create_with_oauth_provider(auth)
2829
find_or_create_user(auth, ALLOWED_PROVIDERS_HASH[:github], auth.info.email)
2930
when ALLOWED_PROVIDERS_HASH[:strava]
3031
find_or_create_user(auth, ALLOWED_PROVIDERS_HASH[:strava], strava_generated_email(auth))
32+
when ALLOWED_PROVIDERS_HASH[:google]
33+
find_or_create_user(auth, ALLOWED_PROVIDERS_HASH[:google], auth.info.email)
3134
else
3235
fail!(error: :invalid_oauth_provider)
3336
end

packs/oauth/app/oauth/controllers/omniauth_callbacks_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ def strava
1111
oauth_sign_in
1212
end
1313

14+
def google_oauth2
15+
oauth_sign_in
16+
end
17+
1418
def oauth_sign_in
1519
result = Oauth::Actors::FindOrCreateUser.result(auth: omniauth_env)
1620

0 commit comments

Comments
 (0)