add oauth with google
This commit is contained in:
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
|
||||
|
||||
Reference in New Issue
Block a user