Module: RailsSimpleAuth::Models::Concerns::TemporaryUser

Extended by:
ActiveSupport::Concern
Defined in:
lib/rails_simple_auth/models/concerns/temporary_user.rb

Instance Method Summary collapse

Instance Method Details

#convert_to_permanent!(email:, password:) ⇒ Object

Convert a temporary user to a permanent user with email and password Returns self on success, false on failure (with errors populated)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
# File 'lib/rails_simple_auth/models/concerns/temporary_user.rb', line 30

def convert_to_permanent!(email:, password:)
  # Validate password presence upfront
  if password.blank?
    errors.add(:password, "can't be blank")
    return false
  end

  # Validate email uniqueness upfront (better UX than failing inside transaction)
  if self.class.where.not(id: id).exists?(email: email)
    errors.add(:email, 'has already been taken')
    return false
  end

  transaction do
    # Reload to discard any unpersisted changes from callbacks before locking
    reload
    lock!

    unless temporary?
      errors.add(:base, 'User is already permanent')
      raise ActiveRecord::Rollback
    end

    attrs = {
      email: email,
      password: password,
      temporary: false
    }
    # Reset confirmation so new email requires verification
    attrs[:confirmed_at] = nil if respond_to?(:confirmed_at)

    raise ActiveRecord::Rollback unless update(attrs)
  end

  # Reload to get actual database state after transaction
  # (in-memory attributes may be stale if transaction was rolled back)
  reload

  # Check if conversion actually succeeded
  return false if errors.any? || temporary?

  invalidate_all_sessions!
  send_conversion_confirmation_email
  Rails.logger.info("[RailsSimpleAuth] Converted temporary user #{id} to permanent")
  self
rescue ActiveRecord::RecordNotUnique
  errors.add(:email, 'has already been taken')
  false
end

#permanent?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/rails_simple_auth/models/concerns/temporary_user.rb', line 24

def permanent?
  !temporary?
end

#temporary?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/rails_simple_auth/models/concerns/temporary_user.rb', line 20

def temporary?
  temporary
end