Skip to content

Commit

Permalink
Set up google auth
Browse files Browse the repository at this point in the history
  • Loading branch information
hmlON committed Jul 8, 2017
1 parent 7d4a7cb commit c9a7946
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .config.reek
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ UncommunicativeModuleName:
exclude:
- Room538

UncommunicativeMethodName:
accept:
- google_oauth2

LongParameterList:
max_params: 4

Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
8 changes: 7 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -366,6 +371,7 @@ DEPENDENCIES
launchy
letter_opener
listen (~> 3.0.5)
omniauth-google-oauth2
omniauth-vkontakte
overcommit
pg (~> 0.18)
Expand All @@ -392,4 +398,4 @@ DEPENDENCIES
web-console (>= 3.3.0)

BUNDLED WITH
1.14.6
1.15.1
12 changes: 12 additions & 0 deletions app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions app/views/devise/registrations/new.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
1 change: 1 addition & 0 deletions app/views/devise/sessions/new.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
1 change: 1 addition & 0 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c9a7946

Please sign in to comment.