Skip to content

Commit

Permalink
Configure devise to use Google sign in
Browse files Browse the repository at this point in the history
Followed the guides provided on omniauth-google-oauth2 repo (https://github.com/zquestz/omniauth-google-oauth2\#devise) and devise wiki (https://github.com/heartcombo/devise/wiki/OmniAuth:-Overview). Note that `data: { turbo: "false" }` is required to make this work, as was noted in a few different Github and Stack Overflow threads (e.g., https://github.com/heartcombo/devise/issues/5236\#issuecomment-1004028752).
  • Loading branch information
Royce Threadgill committed Apr 7, 2022
1 parent c918340 commit f56418c
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ coverage
/node_modules

# Ignore editor config files
.vscode
.vscode

# Ignore .env
.env
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby]
# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false

gem 'devise'
gem "devise"
gem "omniauth-google-oauth2"
gem "omniauth-rails_csrf_protection", "~> 1.0"

# Use Sass to process CSS
# gem "sassc-rails"
Expand Down
28 changes: 28 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ GEM
rubocop-performance
rubocop-rails (~> 2.2.0)
thor
hashie (5.0.0)
httparty (0.20.0)
mime-types (~> 3.0)
multi_xml (>= 0.5.2)
Expand All @@ -179,6 +180,7 @@ GEM
activesupport (>= 5.0.0)
jsbundling-rails (1.0.2)
railties (>= 6.0.0)
jwt (2.3.0)
launchy (2.5.0)
addressable (~> 2.7)
lol_dba (2.1.5)
Expand All @@ -200,6 +202,7 @@ GEM
mini_portile2 (2.8.0)
minitest (5.15.0)
msgpack (1.5.0)
multi_json (1.15.0)
multi_xml (0.6.0)
multipart-post (2.1.1)
net-imap (0.2.3)
Expand All @@ -222,10 +225,31 @@ GEM
racc (~> 1.4)
nokogiri (1.13.3-arm64-darwin)
racc (~> 1.4)
oauth2 (1.4.9)
faraday (>= 0.17.3, < 3.0)
jwt (>= 1.0, < 3.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
octokit (4.22.0)
faraday (>= 0.9)
sawyer (~> 0.8.0, >= 0.5.3)
okcomputer (1.18.4)
omniauth (2.0.4)
hashie (>= 3.4.6)
rack (>= 1.6.2, < 3)
rack-protection
omniauth-google-oauth2 (1.0.1)
jwt (>= 2.0)
oauth2 (~> 1.1)
omniauth (~> 2.0)
omniauth-oauth2 (~> 1.7.1)
omniauth-oauth2 (1.7.2)
oauth2 (~> 1.4)
omniauth (>= 1.9, < 3)
omniauth-rails_csrf_protection (1.0.1)
actionpack (>= 4.2)
omniauth (~> 2.0)
orm_adapter (0.5.0)
parallel (1.22.1)
parser (3.1.1.0)
Expand Down Expand Up @@ -258,6 +282,8 @@ GEM
nio4r (~> 2.0)
racc (1.6.0)
rack (2.2.3)
rack-protection (2.2.0)
rack
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (7.0.2.3)
Expand Down Expand Up @@ -411,6 +437,8 @@ DEPENDENCIES
launchy
lol_dba
okcomputer
omniauth-google-oauth2
omniauth-rails_csrf_protection (~> 1.0)
pg (~> 1.1)
pronto
pronto-brakeman
Expand Down
15 changes: 15 additions & 0 deletions app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
skip_before_action :verify_authenticity_token, only: :google_oauth2

def google_oauth2
@user = User.from_omniauth(request.env['omniauth.auth'])

if @user.persisted?
flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: 'Google'
sign_in_and_redirect @user, event: :authentication
else
session['devise.google_data'] = request.env['omniauth.auth'].except('extra') # Removing extra as it can overflow some session stores
redirect_to new_user_registration_url, alert: @user.errors.full_messages.join("\n")
end
end
end
15 changes: 14 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,18 @@ class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
:recoverable, :rememberable, :validatable,
:omniauthable, omniauth_providers: [:google_oauth2]

def self.from_omniauth(access_token)
data = access_token.info

User.find_by(email: data["email"]) ||
User.create(
uid: access_token["uid"],
provider: access_token["provider"] || nil,
email: data["email"] || nil,
password: Devise.friendly_token[0, 20],
)
end
end
4 changes: 3 additions & 1 deletion app/views/welcome/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<h2 class="text-red-500">Hello World</h2>
<p>
The time is now: <%= Time.now %>
</p>
</p>

<%= button_to "Sign in with Google", user_google_oauth2_omniauth_authorize_path, method: :post, data: { turbo: "false" } %>
1 change: 1 addition & 0 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
# Add a new OmniAuth provider. Check the wiki for more information on setting
# up on your models and hooks.
# config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo'
config.omniauth :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"], { hd: ["gnar.dog"] }

# ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Rails.application.routes.draw do
devise_for :users
devise_for :users, controllers: { omniauth_callbacks: "users/omniauth_callbacks" }
root "welcome#index"
end
6 changes: 6 additions & 0 deletions db/migrate/20220407201654_add_omniauth_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddOmniauthToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :provider, :string
add_column :users, :uid, :string
end
end
4 changes: 3 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f56418c

Please sign in to comment.