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 adds the following options to devise_for:
* +confirm_within+: the time you want to allow the user to access his 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 confirm_within is zero, it means users always have to confirm to sign in.
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
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.
# File lib/devise/models/confirmable.rb, line 62 def active_for_authentication? super && (!confirmation_required? || confirmed? || confirmation_period_valid?) end
Confirm a user by setting its confirmed_at to actual time. If the user is already confirmed, add en error to email field
# File lib/devise/models/confirmable.rb, line 34 def confirm! unless_confirmed do self.confirmation_token = nil self.confirmed_at = Time.now.utc save(:validate => false) end end
Verifies whether a user is confirmed or not
# File lib/devise/models/confirmable.rb, line 43 def confirmed? !!confirmed_at end
The message to be shown if the account is inactive.
# File lib/devise/models/confirmable.rb, line 67 def inactive_message !confirmed? ? :unconfirmed : super end
Resend confirmation token. This method does not need to generate a new token.
# File lib/devise/models/confirmable.rb, line 54 def resend_confirmation_token unless_confirmed { send_confirmation_instructions } end
Send confirmation instructions by email
# File lib/devise/models/confirmable.rb, line 48 def send_confirmation_instructions generate_confirmation_token! if self.confirmation_token.nil? self.devise_mailer.confirmation_instructions(self).deliver end
# File lib/devise/models/confirmable.rb, line 130 def after_password_reset super confirm! unless confirmed? end
Checks if the confirmation for the user is within the limit time. We do this by calculating if the difference between today and the confirmation sent date does not exceed the confirm in time configured. Confirm_within is a model configuration, must always be an integer value.
Example:
# confirm_within = 1.day and confirmation_sent_at = today confirmation_period_valid? # returns true # confirm_within = 5.days and confirmation_sent_at = 4.days.ago confirmation_period_valid? # returns true # confirm_within = 5.days and confirmation_sent_at = 5.days.ago confirmation_period_valid? # returns false # confirm_within = 0.days confirmation_period_valid? # will always return false
# File lib/devise/models/confirmable.rb, line 103 def confirmation_period_valid? confirmation_sent_at && confirmation_sent_at.utc >= self.class.confirm_within.ago end
Callback to overwrite if confirmation is required or not.
# File lib/devise/models/confirmable.rb, line 80 def confirmation_required? !confirmed? end
Generates a new random token for confirmation, and stores the time this token is being generated
# File lib/devise/models/confirmable.rb, line 120 def generate_confirmation_token self.confirmed_at = nil self.confirmation_token = self.class.confirmation_token self.confirmation_sent_at = Time.now.utc end
# File lib/devise/models/confirmable.rb, line 126 def generate_confirmation_token! generate_confirmation_token && save(:validate => false) end
Checks whether the record is confirmed or not, yielding to the block if it's already confirmed, otherwise adds an error to email.
# File lib/devise/models/confirmable.rb, line 109 def unless_confirmed unless confirmed? yield else self.errors.add(:email, :already_confirmed) false end end
Generated with the Darkfish Rdoc Generator 2.