Module: Kingsman::Models::Confirmable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/kingsman/models/confirmable.rb
Overview
Confirmable is responsible to verify if an account is already confirmed to sign in, and to send emails with confirmation instructions. Confirmation instructions are sent to the user email after creating a record and when manually requested by a new confirmation instruction request.
Confirmable tracks the following columns:
-
confirmation_token - A unique random token
-
confirmed_at - A timestamp when the user clicked the confirmation link
-
confirmation_sent_at - A timestamp when the confirmation_token was generated (not sent)
-
unconfirmed_email - An email address copied from the email attr. After confirmation
this value is copied to the email attr then cleared
Options
Confirmable adds the following options to kingsman
:
* +allow_unconfirmed_access_for+: the time you want to allow the user to access their account
before confirming it. After this period, the user access is denied. You can
use this to let your user access some features of your application without
confirming the account, but blocking it after a certain period (ie 7 days).
By default allow_unconfirmed_access_for is zero, it means users always have to confirm to sign in.
* +reconfirmable+: requires any email changes to be confirmed (exactly the same way as
initial account confirmation) to be applied. Requires additional unconfirmed_email
db field to be set up (t.reconfirmable in migrations). Until confirmed, new email is
stored in unconfirmed email column, and copied to email column on successful
confirmation. Also, when used in conjunction with `send_email_changed_notification`,
the notification is sent to the original email when the change is requested,
not when the unconfirmed email is confirmed.
* +confirm_within+: the time before a sent confirmation token becomes invalid.
You can use this to force the user to confirm within a set period of time.
Confirmable will not generate a new token if a repeat confirmation is requested
during this time frame, unless the user's email changed too.
Examples
User.find(1).confirm # returns true unless it's already confirmed
User.find(1).confirmed? # true/false
User.find(1).send_confirmation_instructions # manually send instructions
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#active_for_authentication? ⇒ Boolean
Overwrites active_for_authentication? for confirmation by verifying whether a user is active to sign in or not.
-
#confirm(args = {}) ⇒ Object
Confirm a user by setting it’s confirmed_at to actual time.
-
#confirmed? ⇒ Boolean
Verifies whether a user is confirmed or not.
-
#inactive_message ⇒ Object
The message to be shown if the account is inactive.
- #initialize(*args, &block) ⇒ Object
- #pending_reconfirmation? ⇒ Boolean
-
#resend_confirmation_instructions ⇒ Object
Resend confirmation token.
-
#send_confirmation_instructions ⇒ Object
Send confirmation instructions by email.
- #send_reconfirmation_instructions ⇒ Object
-
#skip_confirmation! ⇒ Object
If you don’t want confirmation to be sent on create, neither a code to be generated, call skip_confirmation!.
-
#skip_confirmation_notification! ⇒ Object
Skips sending the confirmation/reconfirmation notification email after_create/after_update.
-
#skip_reconfirmation! ⇒ Object
If you don’t want reconfirmation to be sent, neither a code to be generated, call skip_reconfirmation!.
Class Method Details
.required_fields(klass) ⇒ Object
70 71 72 73 74 |
# File 'lib/kingsman/models/confirmable.rb', line 70 def self.required_fields(klass) required_methods = [:confirmation_token, :confirmed_at, :confirmation_sent_at] required_methods << :unconfirmed_email if klass.reconfirmable required_methods end |
Instance Method Details
#active_for_authentication? ⇒ Boolean
Overwrites active_for_authentication? for confirmation by verifying whether a user is active to sign in or not. If the user is already confirmed, it should never be blocked. Otherwise we need to calculate if the confirm time has not expired for this user.
144 145 146 |
# File 'lib/kingsman/models/confirmable.rb', line 144 def active_for_authentication? super && (!confirmation_required? || confirmed? || confirmation_period_valid?) end |
#confirm(args = {}) ⇒ Object
Confirm a user by setting it’s confirmed_at to actual time. If the user is already confirmed, add an error to email field. If the user is invalid add errors
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/kingsman/models/confirmable.rb', line 79 def confirm(args = {}) pending_any_confirmation do if confirmation_period_expired? self.errors.add(:email, :confirmation_period_expired, period: Kingsman::TimeInflector.time_ago_in_words(self.class.confirm_within.ago)) return false end self.confirmed_at = Time.now.utc saved = if pending_reconfirmation? skip_reconfirmation! self.email = unconfirmed_email self.unconfirmed_email = nil # We need to validate in such cases to enforce e-mail uniqueness save(validate: true) else save(validate: args[:ensure_valid] == true) end after_confirmation if saved saved end end |
#confirmed? ⇒ Boolean
Verifies whether a user is confirmed or not
106 107 108 |
# File 'lib/kingsman/models/confirmable.rb', line 106 def confirmed? !!confirmed_at end |
#inactive_message ⇒ Object
The message to be shown if the account is inactive.
149 150 151 |
# File 'lib/kingsman/models/confirmable.rb', line 149 def !confirmed? ? :unconfirmed : super end |
#initialize(*args, &block) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/kingsman/models/confirmable.rb', line 61 def initialize(*args, &block) @bypass_confirmation_postpone = false @skip_reconfirmation_in_callback = false @reconfirmation_required = false @skip_confirmation_notification = false @raw_confirmation_token = nil super end |
#pending_reconfirmation? ⇒ Boolean
110 111 112 |
# File 'lib/kingsman/models/confirmable.rb', line 110 def pending_reconfirmation? self.class.reconfirmable && unconfirmed_email.present? end |
#resend_confirmation_instructions ⇒ Object
Resend confirmation token. Regenerates the token if the period is expired.
134 135 136 137 138 |
# File 'lib/kingsman/models/confirmable.rb', line 134 def resend_confirmation_instructions pending_any_confirmation do send_confirmation_instructions end end |
#send_confirmation_instructions ⇒ Object
Send confirmation instructions by email
115 116 117 118 119 120 121 122 |
# File 'lib/kingsman/models/confirmable.rb', line 115 def send_confirmation_instructions unless @raw_confirmation_token generate_confirmation_token! end opts = pending_reconfirmation? ? { to: unconfirmed_email } : { } send_kingsman_notification(:confirmation_instructions, @raw_confirmation_token, opts) end |
#send_reconfirmation_instructions ⇒ Object
124 125 126 127 128 129 130 |
# File 'lib/kingsman/models/confirmable.rb', line 124 def send_reconfirmation_instructions @reconfirmation_required = false unless @skip_confirmation_notification send_confirmation_instructions end end |
#skip_confirmation! ⇒ Object
If you don’t want confirmation to be sent on create, neither a code to be generated, call skip_confirmation!
155 156 157 |
# File 'lib/kingsman/models/confirmable.rb', line 155 def skip_confirmation! self.confirmed_at = Time.now.utc end |
#skip_confirmation_notification! ⇒ Object
Skips sending the confirmation/reconfirmation notification email after_create/after_update. Unlike #skip_confirmation!, record still requires confirmation.
161 162 163 |
# File 'lib/kingsman/models/confirmable.rb', line 161 def skip_confirmation_notification! @skip_confirmation_notification = true end |
#skip_reconfirmation! ⇒ Object
If you don’t want reconfirmation to be sent, neither a code to be generated, call skip_reconfirmation!
167 168 169 |
# File 'lib/kingsman/models/confirmable.rb', line 167 def skip_reconfirmation! @bypass_confirmation_postpone = true end |