Module: RailsSimpleAuth::Models::Concerns::Confirmable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/rails_simple_auth/models/concerns/confirmable.rb
Instance Method Summary collapse
-
#confirm! ⇒ Object
Confirm the user’s email Handles both initial confirmation and reconfirmation (email change) Also sets temporary: false if TemporaryUser concern is included Returns true on success, false on failure (with errors populated).
-
#confirmable_email ⇒ Object
Get the email that needs confirmation.
-
#confirmed? ⇒ Boolean
Check if user has confirmed their email.
-
#generate_confirmation_token ⇒ Object
Generate email confirmation token using Rails signed_id.
-
#reconfirming? ⇒ Boolean
Check if user is changing their email (reconfirmation).
-
#unconfirmed? ⇒ Boolean
Check if user has NOT confirmed their email.
-
#unconfirmed_or_reconfirming? ⇒ Boolean
Check if user needs confirmation (either unconfirmed or reconfirming).
Instance Method Details
#confirm! ⇒ Object
Confirm the user’s email Handles both initial confirmation and reconfirmation (email change) Also sets temporary: false if TemporaryUser concern is included Returns true on success, false on failure (with errors populated)
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 |
# File 'lib/rails_simple_auth/models/concerns/confirmable.rb', line 45 def confirm! attrs = { confirmed_at: Time.current } attrs[:temporary] = false if has_attribute?(:temporary) if reconfirming? # Email change confirmation - check email uniqueness first if self.class.where.not(id: id).exists?(email: unconfirmed_email) errors.add(:email, 'is already taken by another user') return false end attrs[:email] = unconfirmed_email attrs[:unconfirmed_email] = nil update(attrs) elsif unconfirmed? # Initial confirmation update(attrs) else # Already confirmed and not reconfirming true end rescue ActiveRecord::RecordNotUnique # Race condition: email was taken between check and update errors.add(:email, 'is already taken by another user') false end |
#confirmable_email ⇒ Object
Get the email that needs confirmation
37 38 39 |
# File 'lib/rails_simple_auth/models/concerns/confirmable.rb', line 37 def confirmable_email reconfirming? ? unconfirmed_email : email end |
#confirmed? ⇒ Boolean
Check if user has confirmed their email
17 18 19 |
# File 'lib/rails_simple_auth/models/concerns/confirmable.rb', line 17 def confirmed? confirmed_at.present? end |
#generate_confirmation_token ⇒ Object
Generate email confirmation token using Rails signed_id
72 73 74 75 76 77 |
# File 'lib/rails_simple_auth/models/concerns/confirmable.rb', line 72 def generate_confirmation_token signed_id( purpose: :confirm_email, expires_in: RailsSimpleAuth.configuration.confirmation_expiry ) end |
#reconfirming? ⇒ Boolean
Check if user is changing their email (reconfirmation)
27 28 29 |
# File 'lib/rails_simple_auth/models/concerns/confirmable.rb', line 27 def reconfirming? respond_to?(:unconfirmed_email) && unconfirmed_email.present? end |
#unconfirmed? ⇒ Boolean
Check if user has NOT confirmed their email
22 23 24 |
# File 'lib/rails_simple_auth/models/concerns/confirmable.rb', line 22 def unconfirmed? !confirmed? end |
#unconfirmed_or_reconfirming? ⇒ Boolean
Check if user needs confirmation (either unconfirmed or reconfirming)
32 33 34 |
# File 'lib/rails_simple_auth/models/concerns/confirmable.rb', line 32 def unconfirmed_or_reconfirming? unconfirmed? || reconfirming? end |