Skip to content

Commit

Permalink
Extract omniauth methods into concern
Browse files Browse the repository at this point in the history
  • Loading branch information
hmlON committed Jul 29, 2017
1 parent 560e27f commit 2bbc968
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
6 changes: 3 additions & 3 deletions app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ module Users
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def vkontakte
auth = request.env['omniauth.auth']
@user = User.from_omniauth(auth)
@user = OmniauthLogin.perform(auth).user
if @user.persisted?
sign_in_and_redirect @user, event: :authentication # this will throw if @user is not activated
set_flash_message(:notice, :success, kind: 'VK') if is_navigational_format?
else
session['devise.facebook_data'] = auth
session['devise.vkontake_data'] = auth
redirect_to new_user_registration_url
end
end

def google_oauth2
auth = request.env['omniauth.auth']
@user = User.from_omniauth(auth)
@user = OmniauthLogin.perform(auth).user
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?
Expand Down
18 changes: 2 additions & 16 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# User is a preson, who lives in a hostel room
# User is a preson, who lives in a room
#
# == Schema Information
#
Expand Down Expand Up @@ -43,21 +43,7 @@ def room?
room_id != nil
end

# :reek:DuplicateMethodCall { allow_calls: ['auth.info', 'auth.provider', 'auth.uid'] }
def self.from_omniauth(auth)
if where(email: auth.info.email).exists?
user = find_by(email: auth.info.email)
user.update(provider: auth.provider, uid: auth.uid, photo_url: auth.info.image)
user.remember_me!
return user
end

create(email: auth.info.email, name: auth.info.name,
password: Devise.friendly_token[0, 20],
provider: auth.provider, uid: auth.uid,
photo_url: auth.info.image)
end

# method for omniauth
def self.new_with_session(params, session)
super.tap do |user|
data = session['devise.vkontakte_data']
Expand Down
4 changes: 2 additions & 2 deletions app/services/application_service.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# to call: ApplicationService.(params)
#
class ApplicationService
def self.call(*args, **kwargs)
service = new(*args, **kwargs)
def self.perform(args)
service = new(args)
service.perform
service
end
Expand Down
34 changes: 34 additions & 0 deletions app/services/omniauth_login.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# OmniauthLogin finds user by auth or creates new
class OmniauthLogin < ApplicationService
attr_reader :user

# :reek:DuplicateMethodCall { allow_calls: ['auth.info'] }
def initialize(auth)
@params = {}
params[:email] = auth.info.email
params[:name] = auth.info.name
params[:photo_url] = auth.info.image
params[:provider] = auth.provider
params[:uid] = auth.uid
end

def perform
@user = find_user_by_email || create_new_user
end

private

attr_reader :params

def find_user_by_email
user = User.find_by(email: params[:email])
return unless user
user.update(params.select { |key, _| [:provider, :uid, :photo_url].include? key })
user.remember_me!
user
end

def create_new_user
User.create(params.merge(password: Devise.friendly_token[0, 20]))
end
end

0 comments on commit 2bbc968

Please sign in to comment.