Class: AccountCpt::Registration::RegularUser::WithFacebook

Inherits:
Trailblazer::Operation
  • Object
show all
Includes:
ErrorOperationHelper, UserRegistration::MergeGuestBookings
Defined in:
app/concepts/account_cpt/registration/regular_user/with_facebook.rb

Overview

return registered user

Instance Method Summary collapse

Methods included from UserRegistration::MergeGuestBookings

#remove_customer_guest_booking

Methods included from ErrorOperationHelper

#errors, #merge_errors

Instance Method Details

#collect_guest_booking!(options) ⇒ Object



104
105
106
# File 'app/concepts/account_cpt/registration/regular_user/with_facebook.rb', line 104

def collect_guest_booking!(options, *)
  remove_customer_guest_booking(@user.id)
end

#convert_to_long_lived_at(token) ⇒ Object

Returns

"access_token" => "EAA....",
"token_type" => "bearer",
"expires_in" => 5181322

.

Returns:

  • "access_token" => "EAA....",
    "token_type" => "bearer",
    "expires_in" => 5181322
    



99
100
101
102
# File 'app/concepts/account_cpt/registration/regular_user/with_facebook.rb', line 99

def convert_to_long_lived_at(token)
  fb_graph = Koala::Facebook::OAuth.new(Figaro.env.FACEBOOK_APP_ID!, Figaro.env.FACEBOOK_APP_SECRET!)
  fb_graph.exchange_access_token_info(token)
end

#no_email_given!Object



30
31
32
33
# File 'app/concepts/account_cpt/registration/regular_user/with_facebook.rb', line 30

def no_email_given!(*)
  errors.add(:base, 'We require email address for registration')
  true
end

#normalize_params!(options) ⇒ Object



21
22
23
24
25
26
27
28
# File 'app/concepts/account_cpt/registration/regular_user/with_facebook.rb', line 21

def normalize_params!(options, *)
  options['params']['fb_uid'] = options['params']['uid']
  new_fb_token = convert_to_long_lived_at(options['params']['credentials']['token'])['access_token']
  options['params']['fb_access_token'] = new_fb_token
  options['params']['email'] = options['params']['info']['email']
  return false if options['params']['info']['email'].blank?
  true
end

#register_by_facebook!(options) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/concepts/account_cpt/registration/regular_user/with_facebook.rb', line 53

def register_by_facebook!(options, **)
  profile = options['contract.default.params']['extra']['raw_info']

  existing_user = User.find_by(fb_uid: profile['id'])

  # sign in with facebook
  if existing_user.present?
    return true
  end

  # sign up with facebook
  service = UserRegistration::UsingPassword.new(gender: profile['gender'],
                                                name: profile['name'],
                                                # language: profile['locale'],
                                                # calling_code: user_params[:calling_code],
                                                # phone: user_params[:phone],
                                                email: profile['email'],
                                                fb_access_token: options['params']['fb_access_token'],
                                                # r_code: user_params[:r_code],
                                                fb_uid: profile['id'],
                                                password: Devise.friendly_token)

  if service.save
    true
  else
    merge_errors { service.user.errors } if service.user
    false
  end
end

#set_error_messages!(options, options2) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'app/concepts/account_cpt/registration/regular_user/with_facebook.rb', line 83

def set_error_messages!(options, options2, **)
  if options[OpCons::CONTRACT_DEFAULT]
    merge_errors { options[OpCons::CONTRACT_DEFAULT].errors }
  else
    APMErrorHandler.report('Facebook sign up error', options: options, options2: options2)
    errors.add(:base, 'Something went wrong')
  end
  true
end

#success!(options) ⇒ Object



49
50
51
# File 'app/concepts/account_cpt/registration/regular_user/with_facebook.rb', line 49

def success!(options, **)
  options[OpCons::OPERATION_RESULT] = @user
end

#update_user_uid_and_provider!(options) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/concepts/account_cpt/registration/regular_user/with_facebook.rb', line 35

def update_user_uid_and_provider!(options, *)
  uid = options['params']['uid']
  email = options['params']['email']
  @user = User.find_by(uid: uid) || User.find_by(email: email)

  if @user.blank?
    errors.add(:base, 'email not found')
    return false
  end

  @user.attributes = { email: email, uid: uid, provider: :facebook }
  @user.save
end