Class: Spree::UserIdentity

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree/user_identity.rb

Constant Summary

Constants included from PrefixedId

PrefixedId::SQIDS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

belongs_to_required_by_default, #can_be_deleted?, for_store, has_many_inversing, json_api_columns, json_api_permitted_attributes, json_api_type, page, #slug_candidates, spree_base_scopes, spree_base_uniqueness_scope, to_tom_select_json, #uuid_for_friendly_id

Methods included from PrefixedId

#prefixed_id, #to_param

Methods included from Publishable

#event_context, #event_payload, #event_prefix, #event_serializer_class, #publish_event

Methods included from IntegrationsConcern

#store_integration, #store_integrations

Methods included from Preferences::Preferable

#clear_preferences, #default_preferences, #defined_preferences, #deprecated_preferences, #get_preference, #has_preference!, #has_preference?, #preference_change, #preference_default, #preference_deprecated, #preference_type, #preferences_of_type, #restore_preferences_for, #set_preference

Class Method Details

.create_user_from_oauth(provider:, uid:, info:, tokens: {}, user_class: nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/spree/user_identity.rb', line 51

def self.create_user_from_oauth(provider:, uid:, info:, tokens: {}, user_class: nil)
  user_class ||= Spree.user_class

  user = user_class.create!(
    email: info[:email] || generate_temp_email(provider, uid),
    password: SecureRandom.hex(32), # Random password for OAuth users
    first_name: info[:first_name],
    last_name: info[:last_name]
  )

  user.identities.create!(
    provider: provider,
    uid: uid,
    info: info,
    access_token: tokens[:access_token],
    refresh_token: tokens[:refresh_token],
    expires_at: tokens[:expires_at]
  )

  user
end

.find_or_create_from_oauth(provider:, uid:, info:, tokens: {}, user_class: nil) ⇒ Object

Find or create user from OAuth data



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/spree/user_identity.rb', line 24

def self.find_or_create_from_oauth(provider:, uid:, info:, tokens: {}, user_class: nil)
  user_class ||= Spree.user_class
  user_type = user_class.name

  identity = find_by(provider: provider, uid: uid, user_type: user_type)

  if identity
    # Update existing identity with fresh tokens
    identity.update(
      info: info,
      access_token: tokens[:access_token],
      refresh_token: tokens[:refresh_token],
      expires_at: tokens[:expires_at]
    )
    identity.user
  else
    # Create new user and identity
    create_user_from_oauth(
      provider: provider,
      uid: uid,
      info: info,
      tokens: tokens,
      user_class: user_class
    )
  end
end

.generate_temp_email(provider, uid) ⇒ Object



73
74
75
# File 'app/models/spree/user_identity.rb', line 73

def self.generate_temp_email(provider, uid)
  "#{provider}-#{uid}@temporary.example.com"
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'app/models/spree/user_identity.rb', line 77

def expired?
  expires_at && expires_at < Time.current
end