-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure devise to use Google sign in
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
Showing
10 changed files
with
78 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,4 +42,7 @@ coverage | |
/node_modules | ||
|
||
# Ignore editor config files | ||
.vscode | ||
.vscode | ||
|
||
# Ignore .env | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.