Class: AccountCpt::Session::StaffUser::WithGoogle

Inherits:
Trailblazer::Operation
  • Object
show all
Includes:
ErrorOperationHelper
Defined in:
app/concepts/account_cpt/session/staff_user/with_google.rb

Overview

return registered user

Instance Method Summary collapse

Methods included from ErrorOperationHelper

#errors, #merge_errors

Instance Method Details

#normalize_params!(options) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/concepts/account_cpt/session/staff_user/with_google.rb', line 13

def normalize_params!(options)
  original_params = options['params'].dup
  options['original_params'] = original_params
  options['params'] = {
    'provider' => 'google_oauth2',
    'fb_uid' => original_params['uid'],
    'email' => original_params['info']['email'],
    'username' => original_params['info']['name'],
    'fb_access_token' => original_params['credentials']['token']
  }
  unless options['params']['email'].include?('@hungryhub.com')
    errors.add(:base, 'Your account is not valid')
    return false
  end
  true
end

#register_with_google!(options) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/concepts/account_cpt/session/staff_user/with_google.rb', line 30

def register_with_google!(options, **)
  user = User.find_or_initialize_by email: options['params']['email']
  if user.new_record?
    user.assign_attributes options['params']
    user.password = Devise.friendly_token
    unless user.valid?
       merge_errors { user.errors }
       return false
    end
  else
    user.update_attributes provider: :google_oauth2
  end
  options[OpCons::OPERATION_RESULT] = user
  true
end

#set_error_messages!(options, options2) ⇒ Object



46
47
48
49
50
51
52
# File 'app/concepts/account_cpt/session/staff_user/with_google.rb', line 46

def set_error_messages!(options, options2, **)
  APMErrorHandler.report('Google sign in error', options: options, options2: options2)
  if errors.messages.length.zero?
    errors.add(:base, 'Something went wrong')
  end
  true
end