Class: SpreeCmCommissioner::UserRegistrationWithIdToken

Inherits:
BaseInteractor
  • Object
show all
Defined in:
app/interactors/spree_cm_commissioner/user_registration_with_id_token.rb

Instance Method Summary collapse

Instance Method Details

#callObject

: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])
      link_user_account!(firebase_context.provider)
    end
  else
    context.fail!(message: firebase_context.message)
  end
end

identity_type: identity_type,
sub: sub



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/interactors/spree_cm_commissioner/user_registration_with_id_token.rb', line 48

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.full_messages)
  end
end

#name_attributes(name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/interactors/spree_cm_commissioner/user_registration_with_id_token.rb', line 26

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) ⇒ Object



17
18
19
20
21
22
23
24
# File 'app/interactors/spree_cm_commissioner/user_registration_with_id_token.rb', line 17

def register_user!(name)
  user = Spree.user_class.new(password: SecureRandom.base64(16), **name_attributes(name))
  if user.save(validate: false)
    context.user = user
  else
    context.fail!(message: user.errors.full_messages.join('\n'))
  end
end