add oauth with google

This commit is contained in:
João Geonizeli
2022-07-20 22:22:41 -03:00
parent 072e1de01b
commit ae7d31bc1b
9 changed files with 74 additions and 6 deletions

2
.env.example Normal file
View File

@@ -0,0 +1,2 @@
GOOGLE_OAUTH_CLIENT_ID=test
GOOGLE_OAUTH_CLIENT_SECRET=test

2
.gitignore vendored
View File

@@ -29,3 +29,5 @@
# Ignore master key for decrypting credentials and more. # Ignore master key for decrypting credentials and more.
/config/master.key /config/master.key
.env

View File

@@ -27,8 +27,11 @@ gem "bootsnap", require: false
# gem "image_processing", "~> 1.2" # gem "image_processing", "~> 1.2"
gem "devise", "~> 4.8" gem "devise", "~> 4.8"
gem "omniauth", "~> 1.9.1"
gem "omniauth-google-oauth2", "~> 0.8.2"
group :development, :test do group :development, :test do
gem "dotenv-rails", "~> 2.7"
gem "rspec-rails", "~> 5.1" gem "rspec-rails", "~> 5.1"
gem "factory_bot_rails", "~> 6.2" gem "factory_bot_rails", "~> 6.2"

View File

@@ -87,14 +87,23 @@ GEM
warden (~> 1.2.3) warden (~> 1.2.3)
diff-lcs (1.5.0) diff-lcs (1.5.0)
digest (3.1.0) digest (3.1.0)
dotenv (2.7.6)
dotenv-rails (2.7.6)
dotenv (= 2.7.6)
railties (>= 3.2)
erubi (1.10.0) erubi (1.10.0)
factory_bot (6.2.1) factory_bot (6.2.1)
activesupport (>= 5.0.0) activesupport (>= 5.0.0)
factory_bot_rails (6.2.0) factory_bot_rails (6.2.0)
factory_bot (~> 6.2.0) factory_bot (~> 6.2.0)
railties (>= 5.0.0) railties (>= 5.0.0)
faraday (2.3.0)
faraday-net_http (~> 2.0)
ruby2_keywords (>= 0.0.4)
faraday-net_http (2.0.3)
globalid (1.0.0) globalid (1.0.0)
activesupport (>= 5.0) activesupport (>= 5.0)
hashie (5.0.0)
i18n (1.12.0) i18n (1.12.0)
concurrent-ruby (~> 1.0) concurrent-ruby (~> 1.0)
importmap-rails (1.1.5) importmap-rails (1.1.5)
@@ -106,6 +115,7 @@ GEM
jbuilder (2.11.5) jbuilder (2.11.5)
actionview (>= 5.0.0) actionview (>= 5.0.0)
activesupport (>= 5.0.0) activesupport (>= 5.0.0)
jwt (2.4.1)
loofah (2.18.0) loofah (2.18.0)
crass (~> 1.0.2) crass (~> 1.0.2)
nokogiri (>= 1.5.9) nokogiri (>= 1.5.9)
@@ -116,6 +126,8 @@ GEM
mini_mime (1.1.2) mini_mime (1.1.2)
minitest (5.16.2) minitest (5.16.2)
msgpack (1.5.3) msgpack (1.5.3)
multi_json (1.15.0)
multi_xml (0.6.0)
net-imap (0.2.3) net-imap (0.2.3)
digest digest
net-protocol net-protocol
@@ -133,6 +145,23 @@ GEM
nio4r (2.5.8) nio4r (2.5.8)
nokogiri (1.13.7-x86_64-linux) nokogiri (1.13.7-x86_64-linux)
racc (~> 1.4) racc (~> 1.4)
oauth2 (1.4.10)
faraday (>= 0.17.3, < 3.0)
jwt (>= 1.0, < 3.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
omniauth (1.9.1)
hashie (>= 3.4.6)
rack (>= 1.6.2, < 3)
omniauth-google-oauth2 (0.8.2)
jwt (>= 2.0)
oauth2 (~> 1.1)
omniauth (~> 1.1)
omniauth-oauth2 (>= 1.6)
omniauth-oauth2 (1.7.3)
oauth2 (>= 1.4, < 3)
omniauth (>= 1.9, < 3)
orm_adapter (0.5.0) orm_adapter (0.5.0)
pg (1.4.1) pg (1.4.1)
puma (5.6.4) puma (5.6.4)
@@ -191,6 +220,7 @@ GEM
rspec-mocks (~> 3.10) rspec-mocks (~> 3.10)
rspec-support (~> 3.10) rspec-support (~> 3.10)
rspec-support (3.11.0) rspec-support (3.11.0)
ruby2_keywords (0.0.5)
sprockets (4.1.1) sprockets (4.1.1)
concurrent-ruby (~> 1.0) concurrent-ruby (~> 1.0)
rack (> 1, < 3) rack (> 1, < 3)
@@ -229,9 +259,12 @@ DEPENDENCIES
bootsnap bootsnap
debug debug
devise (~> 4.8) devise (~> 4.8)
dotenv-rails (~> 2.7)
factory_bot_rails (~> 6.2) factory_bot_rails (~> 6.2)
importmap-rails importmap-rails
jbuilder jbuilder
omniauth (~> 1.9.1)
omniauth-google-oauth2 (~> 0.8.2)
pg (~> 1.1) pg (~> 1.1)
puma (~> 5.0) puma (~> 5.0)
rails (~> 7.0.3, >= 7.0.3.1) rails (~> 7.0.3, >= 7.0.3.1)

View File

@@ -0,0 +1,16 @@
module Users
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def google_oauth2
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.find_by!(email: request.env['omniauth.auth'].info['email'])
flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: 'Google'
sign_in_and_redirect @user, event: :authentication
rescue ActiveRecord::RecordNotFound => e
# Removing extra as it can overflow some session stores
session['devise.google_data'] = request.env['omniauth.auth'].except('extra')
redirect_to new_user_registration_url, alert: e.message
end
end
end

View File

@@ -18,10 +18,13 @@
# index_users_on_reset_password_token (reset_password_token) UNIQUE # index_users_on_reset_password_token (reset_password_token) UNIQUE
# #
class User < ApplicationRecord class User < ApplicationRecord
# Include default devise modules. Others available are: devise :database_authenticatable,
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable :registerable,
devise :database_authenticatable, :registerable, :recoverable,
:recoverable, :rememberable, :validatable :rememberable,
:validatable,
:omniauthable,
omniauth_providers: [:google_oauth2]
validates :name, presence: true validates :name, presence: true
end end

View File

@@ -18,6 +18,10 @@ require "action_cable/engine"
# you've limited to :test, :development, or :production. # you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups) Bundler.require(*Rails.groups)
if ['development', 'test'].include? ENV['RAILS_ENV']
Dotenv::Railtie.load
end
module ProgressTest module ProgressTest
class Application < Rails::Application class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version. # Initialize configuration defaults for originally generated Rails version.

View File

@@ -271,7 +271,12 @@ Devise.setup do |config|
# ==> OmniAuth # ==> OmniAuth
# Add a new OmniAuth provider. Check the wiki for more information on setting # Add a new OmniAuth provider. Check the wiki for more information on setting
# up on your models and hooks. # up on your models and hooks.
# config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo' config.omniauth(
:google_oauth2,
ENV['GOOGLE_OAUTH_CLIENT_ID'],
ENV['GOOGLE_OAUTH_CLIENT_SECRET'],
{}
)
# ==> Warden configuration # ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or # If you want to use other strategies, that are not supported by Devise, or

View File

@@ -1,5 +1,5 @@
Rails.application.routes.draw do Rails.application.routes.draw do
devise_for :users devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
# Defines the root path route ("/") # Defines the root path route ("/")