Class: SpreeCmCommissioner::UserRegistrationWithIdToken
- Inherits:
-
BaseInteractor
- Object
- BaseInteractor
- SpreeCmCommissioner::UserRegistrationWithIdToken
- Defined in:
- app/interactors/spree_cm_commissioner/user_registration_with_id_token.rb
Instance Method Summary collapse
-
#call ⇒ Object
:id_token.
-
#link_user_account!(provider) ⇒ Object
{ identity_type: identity_type, sub: sub }.
- #name_attributes(name) ⇒ Object
- #register_user!(name, email) ⇒ Object
Instance Method Details
#call ⇒ Object
:id_token
4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'app/interactors/spree_cm_commissioner/user_registration_with_id_token.rb', line 4 def call firebase_context = SpreeCmCommissioner::FirebaseIdTokenProvider.call(id_token: context.id_token) if firebase_context.success? ActiveRecord::Base.transaction do register_user!(firebase_context.provider[:name], firebase_context.provider[:email]) link_user_account!(firebase_context.provider) end else context.fail!(message: firebase_context.) end end |
#link_user_account!(provider) ⇒ Object
identity_type: identity_type,
sub: sub
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/interactors/spree_cm_commissioner/user_registration_with_id_token.rb', line 54 def link_user_account!(provider) identity_type = SpreeCmCommissioner::UserIdentityProvider.identity_types[provider[:identity_type]] user_identity_provider = SpreeCmCommissioner::UserIdentityProvider.where( user_id: context.user, identity_type: identity_type ).first_or_initialize user_identity_provider.sub = provider[:sub] user_identity_provider.email = provider[:email] user_identity_provider.name = provider[:name] if user_identity_provider.save context.user_identity_provider = user_identity_provider else context.fail!(message: user_identity_provider.errors.) end end |
#name_attributes(name) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/interactors/spree_cm_commissioner/user_registration_with_id_token.rb', line 32 def name_attributes(name) full_name = name&.strip return {} if full_name.blank? split = full_name.split first_name = split[0] last_name = split[1..].join(' ') attributes = {} attributes[:first_name] = first_name if first_name.present? attributes[:last_name] = last_name if last_name.present? attributes end |
#register_user!(name, email) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'app/interactors/spree_cm_commissioner/user_registration_with_id_token.rb', line 17 def register_user!(name, email) user = Spree.user_class.new( password: SecureRandom.base64(16), email: email, tenant_id: context.tenant_id, **name_attributes(name) ) if user.save(validate: false) context.user = user else context.fail!(message: user.errors..join('\n')) end end |