add oauth with google
This commit is contained in:
2
.env.example
Normal file
2
.env.example
Normal file
@@ -0,0 +1,2 @@
|
||||
GOOGLE_OAUTH_CLIENT_ID=test
|
||||
GOOGLE_OAUTH_CLIENT_SECRET=test
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -29,3 +29,5 @@
|
||||
|
||||
# Ignore master key for decrypting credentials and more.
|
||||
/config/master.key
|
||||
|
||||
.env
|
||||
3
Gemfile
3
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"
|
||||
|
||||
|
||||
33
Gemfile.lock
33
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)
|
||||
|
||||
16
app/controllers/users/omniauth_callbacks_controller.rb
Normal file
16
app/controllers/users/omniauth_callbacks_controller.rb
Normal 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
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 ("/")
|
||||
|
||||
Reference in New Issue
Block a user