From ae7d31bc1bf429d731b0e18b821dfe34e52a73df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Geonizeli?= Date: Wed, 20 Jul 2022 22:22:41 -0300 Subject: [PATCH] add oauth with google --- .env.example | 2 ++ .gitignore | 2 ++ Gemfile | 3 ++ Gemfile.lock | 33 +++++++++++++++++++ .../users/omniauth_callbacks_controller.rb | 16 +++++++++ app/models/user.rb | 11 ++++--- config/application.rb | 4 +++ config/initializers/devise.rb | 7 +++- config/routes.rb | 2 +- 9 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 .env.example create mode 100644 app/controllers/users/omniauth_callbacks_controller.rb diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..15101bb --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +GOOGLE_OAUTH_CLIENT_ID=test +GOOGLE_OAUTH_CLIENT_SECRET=test \ No newline at end of file diff --git a/.gitignore b/.gitignore index e16dc71..85fe9d1 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,5 @@ # Ignore master key for decrypting credentials and more. /config/master.key + +.env \ No newline at end of file diff --git a/Gemfile b/Gemfile index 17f7c6e..c942df4 100644 --- a/Gemfile +++ b/Gemfile @@ -27,8 +27,11 @@ gem "bootsnap", require: false # gem "image_processing", "~> 1.2" gem "devise", "~> 4.8" +gem "omniauth", "~> 1.9.1" +gem "omniauth-google-oauth2", "~> 0.8.2" group :development, :test do + gem "dotenv-rails", "~> 2.7" gem "rspec-rails", "~> 5.1" gem "factory_bot_rails", "~> 6.2" diff --git a/Gemfile.lock b/Gemfile.lock index 2880b38..a41ed96 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -87,14 +87,23 @@ GEM warden (~> 1.2.3) diff-lcs (1.5.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) factory_bot (6.2.1) activesupport (>= 5.0.0) factory_bot_rails (6.2.0) factory_bot (~> 6.2.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) activesupport (>= 5.0) + hashie (5.0.0) i18n (1.12.0) concurrent-ruby (~> 1.0) importmap-rails (1.1.5) @@ -106,6 +115,7 @@ GEM jbuilder (2.11.5) actionview (>= 5.0.0) activesupport (>= 5.0.0) + jwt (2.4.1) loofah (2.18.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) @@ -116,6 +126,8 @@ GEM mini_mime (1.1.2) minitest (5.16.2) msgpack (1.5.3) + multi_json (1.15.0) + multi_xml (0.6.0) net-imap (0.2.3) digest net-protocol @@ -133,6 +145,23 @@ GEM nio4r (2.5.8) nokogiri (1.13.7-x86_64-linux) 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) pg (1.4.1) puma (5.6.4) @@ -191,6 +220,7 @@ GEM rspec-mocks (~> 3.10) rspec-support (~> 3.10) rspec-support (3.11.0) + ruby2_keywords (0.0.5) sprockets (4.1.1) concurrent-ruby (~> 1.0) rack (> 1, < 3) @@ -229,9 +259,12 @@ DEPENDENCIES bootsnap debug devise (~> 4.8) + dotenv-rails (~> 2.7) factory_bot_rails (~> 6.2) importmap-rails jbuilder + omniauth (~> 1.9.1) + omniauth-google-oauth2 (~> 0.8.2) pg (~> 1.1) puma (~> 5.0) rails (~> 7.0.3, >= 7.0.3.1) diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb new file mode 100644 index 0000000..b7e0224 --- /dev/null +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -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 \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index 7ee67a6..201c27e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -18,10 +18,13 @@ # index_users_on_reset_password_token (reset_password_token) UNIQUE # class User < ApplicationRecord - # Include default devise modules. Others available are: - # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable - devise :database_authenticatable, :registerable, - :recoverable, :rememberable, :validatable + devise :database_authenticatable, + :registerable, + :recoverable, + :rememberable, + :validatable, + :omniauthable, + omniauth_providers: [:google_oauth2] validates :name, presence: true end diff --git a/config/application.rb b/config/application.rb index 770e7c1..7044011 100644 --- a/config/application.rb +++ b/config/application.rb @@ -18,6 +18,10 @@ require "action_cable/engine" # you've limited to :test, :development, or :production. Bundler.require(*Rails.groups) +if ['development', 'test'].include? ENV['RAILS_ENV'] + Dotenv::Railtie.load +end + module ProgressTest class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 9b379f5..77509ac 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -271,7 +271,12 @@ Devise.setup do |config| # ==> OmniAuth # 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_OAUTH_CLIENT_ID'], + ENV['GOOGLE_OAUTH_CLIENT_SECRET'], + {} + ) # ==> Warden configuration # If you want to use other strategies, that are not supported by Devise, or diff --git a/config/routes.rb b/config/routes.rb index 3bd5343..ec62a90 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ 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 # Defines the root path route ("/")