From c9a794661760ea9abf986ff7e8650f39057003ce Mon Sep 17 00:00:00 2001 From: Nikita Kholin Date: Sat, 8 Jul 2017 11:16:02 +0300 Subject: [PATCH] Set up google auth --- .config.reek | 4 ++++ Gemfile | 1 + Gemfile.lock | 8 +++++++- .../users/omniauth_callbacks_controller.rb | 12 ++++++++++++ app/models/user.rb | 2 +- app/views/devise/registrations/new.html.slim | 1 + app/views/devise/sessions/new.html.slim | 1 + config/initializers/devise.rb | 1 + 8 files changed, 28 insertions(+), 2 deletions(-) diff --git a/.config.reek b/.config.reek index 5079c22..590dacc 100644 --- a/.config.reek +++ b/.config.reek @@ -9,6 +9,10 @@ UncommunicativeModuleName: exclude: - Room538 +UncommunicativeMethodName: + accept: + - google_oauth2 + LongParameterList: max_params: 4 diff --git a/Gemfile b/Gemfile index da681e9..309e08d 100644 --- a/Gemfile +++ b/Gemfile @@ -13,6 +13,7 @@ gem 'devise' gem 'font-awesome-rails' gem 'jbuilder', '~> 2.5' gem 'jquery-rails' +gem 'omniauth-google-oauth2' gem 'omniauth-vkontakte' gem 'pg', '~> 0.18' gem 'public_activity' diff --git a/Gemfile.lock b/Gemfile.lock index c58491b..9ffe373 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -175,6 +175,11 @@ GEM omniauth (1.6.1) hashie (>= 3.4.6, < 3.6.0) rack (>= 1.6.2, < 3) + omniauth-google-oauth2 (0.5.0) + jwt (~> 1.5) + multi_json (~> 1.3) + omniauth (>= 1.1.1) + omniauth-oauth2 (>= 1.3.1) omniauth-oauth2 (1.4.0) oauth2 (~> 1.0) omniauth (~> 1.2) @@ -366,6 +371,7 @@ DEPENDENCIES launchy letter_opener listen (~> 3.0.5) + omniauth-google-oauth2 omniauth-vkontakte overcommit pg (~> 0.18) @@ -392,4 +398,4 @@ DEPENDENCIES web-console (>= 3.3.0) BUNDLED WITH - 1.14.6 + 1.15.1 diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb index dfcac34..4ee2a86 100644 --- a/app/controllers/users/omniauth_callbacks_controller.rb +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -13,6 +13,18 @@ def vkontakte end end + def google_oauth2 + auth = request.env['omniauth.auth'] + @user = User.from_omniauth(auth) + if @user.persisted? + sign_in_and_redirect @user, event: :authentication # this will throw if @user is not activated + set_flash_message(:notice, :success, kind: 'Google') if is_navigational_format? + else + session['devise.google_data'] = auth + redirect_to new_user_registration_url + end + end + def failure redirect_to root_path end diff --git a/app/models/user.rb b/app/models/user.rb index 9cf0825..1d81772 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -9,7 +9,7 @@ class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, - :omniauthable, omniauth_providers: [:vkontakte] + :omniauthable, omniauth_providers: [:google_oauth2, :vkontakte] validates :name, presence: true diff --git a/app/views/devise/registrations/new.html.slim b/app/views/devise/registrations/new.html.slim index 14b0174..aa96d81 100644 --- a/app/views/devise/registrations/new.html.slim +++ b/app/views/devise/registrations/new.html.slim @@ -4,6 +4,7 @@ h2.text-center Sign up .omniauth-links += link_to "Sign up with #{fa_icon('google')}".html_safe, user_google_oauth2_omniauth_authorize_path, class: 'omniauth-link' = link_to "Sign up with #{fa_icon('vk')}".html_safe, user_vkontakte_omniauth_authorize_path, class: 'omniauth-link' = simple_form_for(resource, as: resource_name, url: registration_path(resource_name), defaults: { required: true, label: false }) do |f| diff --git a/app/views/devise/sessions/new.html.slim b/app/views/devise/sessions/new.html.slim index 1374c9d..69f2986 100644 --- a/app/views/devise/sessions/new.html.slim +++ b/app/views/devise/sessions/new.html.slim @@ -4,6 +4,7 @@ h2.text-center Sign in .omniauth-links + = link_to "Sign in with #{fa_icon('google')}".html_safe, user_google_oauth2_omniauth_authorize_path, class: 'omniauth-link' = link_to "Sign in with #{fa_icon('vk')}".html_safe, user_vkontakte_omniauth_authorize_path, class: 'omniauth-link' = simple_form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 861c89e..8497d26 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -254,6 +254,7 @@ scope: 'email', lang: 'en', display: 'popup' + config.omniauth :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET'], {} # ==> Warden configuration # If you want to use other strategies, that are not supported by Devise, or