Module: Devise::Models::Confirmable

Extended by:
ActiveSupport::Concern
Includes:
ActionView::Helpers::DateHelper
Defined in:
lib/devise/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.

Options

Confirmable adds the following options to devise_for:

* +allow_unconfirmed_access_for+: the time you want to allow the user to access his 
  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 , 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  confirmation) to be applied. Requires additional unconfirmed_email
  db field to be setup (t.reconfirmable in migrations). Until confirmed new email is
  stored in unconfirmed email column, and copied to email column on successful
  confirmation.
* +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.

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

Class Method Details

.required_fields(klass) ⇒ Object



49
50
51
52
53
# File 'lib/devise/models/confirmable.rb', line 49

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.

Returns:

  • (Boolean)


119
120
121
# File 'lib/devise/models/confirmable.rb', line 119

def active_for_authentication?
  super && (!confirmation_required? || confirmed? || confirmation_period_valid?)
end

#after_password_resetObject (protected)



226
227
228
229
# File 'lib/devise/models/confirmable.rb', line 226

def after_password_reset
  super
  confirm! unless confirmed?
end

#confirm!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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/devise/models/confirmable.rb', line 58

def confirm!
  pending_any_confirmation do
    if confirmation_period_expired?
      self.errors.add(:email, :confirmation_period_expired,
        :period => Devise::TimeInflector.time_ago_in_words(self.class.confirm_within.ago))
      return false
    end

    self.confirmation_token = nil
    self.confirmed_at = Time.now.utc

    if self.class.reconfirmable && unconfirmed_email.present?
      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 => false)
    end
  end
end

#confirmation_period_expired?Boolean (protected)

Checks if the user confirmation happens before the token becomes invalid Examples:

# confirm_within = 3.days and confirmation_sent_at = 2.days.ago
confirmation_period_expired?  # returns false

# confirm_within = 3.days and confirmation_sent_at = 4.days.ago
confirmation_period_expired?  # returns true

# confirm_within = nil
confirmation_period_expired?  # will always return false

Returns:

  • (Boolean)


201
202
203
# File 'lib/devise/models/confirmable.rb', line 201

def confirmation_period_expired?
  self.class.confirm_within && (Time.now > self.confirmation_sent_at + self.class.confirm_within )
end

#confirmation_period_valid?Boolean (protected)

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:

# allow_unconfirmed_access_for = 1.day and confirmation_sent_at = today
confirmation_period_valid?   # returns true

# allow_unconfirmed_access_for = 5.days and confirmation_sent_at = 4.days.ago
confirmation_period_valid?   # returns true

# allow_unconfirmed_access_for = 5.days and confirmation_sent_at = 5.days.ago
confirmation_period_valid?   # returns false

# allow_unconfirmed_access_for = 0.days
confirmation_period_valid?   # will always return false

# allow_unconfirmed_access_for = nil
confirmation_period_valid?   # will always return true

Returns:

  • (Boolean)


185
186
187
# File 'lib/devise/models/confirmable.rb', line 185

def confirmation_period_valid?
  self.class.allow_unconfirmed_access_for.nil? || (confirmation_sent_at && confirmation_sent_at.utc >= self.class.allow_unconfirmed_access_for.ago)
end

#confirmation_required?Boolean (protected)

Callback to overwrite if confirmation is required or not.

Returns:

  • (Boolean)


159
160
161
# File 'lib/devise/models/confirmable.rb', line 159

def confirmation_required?
  !confirmed?
end

#confirmed?Boolean

Verifies whether a user is confirmed or not

Returns:

  • (Boolean)


83
84
85
# File 'lib/devise/models/confirmable.rb', line 83

def confirmed?
  !!confirmed_at
end

#ensure_confirmation_token!Object

Generate a confirmation token unless already exists and save the record.



111
112
113
# File 'lib/devise/models/confirmable.rb', line 111

def ensure_confirmation_token!
  generate_confirmation_token! if should_generate_confirmation_token?
end

#generate_confirmation_tokenObject (protected)

Generates a new random token for confirmation, and stores the time this token is being generated



217
218
219
220
# File 'lib/devise/models/confirmable.rb', line 217

def generate_confirmation_token
  self.confirmation_token = self.class.confirmation_token
  self.confirmation_sent_at = Time.now.utc
end

#generate_confirmation_token!Object (protected)



222
223
224
# File 'lib/devise/models/confirmable.rb', line 222

def generate_confirmation_token!
  generate_confirmation_token && save(:validate => false)
end

#inactive_messageObject

The message to be shown if the account is inactive.



124
125
126
# File 'lib/devise/models/confirmable.rb', line 124

def inactive_message
  !confirmed? ? :unconfirmed : super
end

#initialize(*args, &block) ⇒ Object



42
43
44
45
46
47
# File 'lib/devise/models/confirmable.rb', line 42

def initialize(*args, &block)
  @bypass_postpone = false
  @reconfirmation_required = false
  @skip_confirmation_notification = false
  super
end

#pending_any_confirmationObject (protected)

Checks whether the record requires any confirmation.



206
207
208
209
210
211
212
213
# File 'lib/devise/models/confirmable.rb', line 206

def pending_any_confirmation
  if (!confirmed? || pending_reconfirmation?)
    yield
  else
    self.errors.add(:email, :already_confirmed)
    false
  end
end

#pending_reconfirmation?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/devise/models/confirmable.rb', line 87

def pending_reconfirmation?
  self.class.reconfirmable && unconfirmed_email.present?
end

#postpone_email_change?Boolean (protected)

Returns:

  • (Boolean)


237
238
239
240
241
# File 'lib/devise/models/confirmable.rb', line 237

def postpone_email_change?
  postpone = self.class.reconfirmable && email_changed? && !@bypass_postpone && !self.email.blank?
  @bypass_postpone = false
  postpone
end

#postpone_email_change_until_confirmationObject (protected)



231
232
233
234
235
# File 'lib/devise/models/confirmable.rb', line 231

def postpone_email_change_until_confirmation
  @reconfirmation_required = true
  self.unconfirmed_email = self.email
  self.email = self.email_was
end

#reconfirmation_required?Boolean (protected)

Returns:

  • (Boolean)


243
244
245
# File 'lib/devise/models/confirmable.rb', line 243

def reconfirmation_required?
  self.class.reconfirmable && @reconfirmation_required && !self.email.blank?
end

#resend_confirmation_tokenObject

Resend confirmation token. This method does not need to generate a new token.



103
104
105
106
107
108
# File 'lib/devise/models/confirmable.rb', line 103

def resend_confirmation_token
  pending_any_confirmation do
    self.confirmation_token = nil if confirmation_period_expired?
    send_confirmation_instructions
  end
end

#send_confirmation_instructionsObject

Send confirmation instructions by email



92
93
94
95
96
97
98
99
100
# File 'lib/devise/models/confirmable.rb', line 92

def send_confirmation_instructions
  self.confirmation_token = nil if reconfirmation_required?
  @reconfirmation_required = false

  ensure_confirmation_token!

  opts = pending_reconfirmation? ? { :to => unconfirmed_email } : { }
  send_devise_notification(:confirmation_instructions, opts)
end

#send_confirmation_notification?Boolean (protected)

Returns:

  • (Boolean)


247
248
249
# File 'lib/devise/models/confirmable.rb', line 247

def send_confirmation_notification?
  confirmation_required? && !@skip_confirmation_notification && !self.email.blank?
end

#send_on_create_confirmation_instructionsObject (protected)

A callback method used to deliver confirmation instructions on creation. This can be overriden in models to map to a nice sign up e-mail.



154
155
156
# File 'lib/devise/models/confirmable.rb', line 154

def send_on_create_confirmation_instructions
  send_devise_notification(:confirmation_instructions)
end

#should_generate_confirmation_token?Boolean (protected)

Returns:

  • (Boolean)


147
148
149
# File 'lib/devise/models/confirmable.rb', line 147

def should_generate_confirmation_token?
  confirmation_token.nil? || confirmation_period_expired?
end

#skip_confirmation!Object

If you don’t want confirmation to be sent on create, neither a code to be generated, call skip_confirmation!



130
131
132
# File 'lib/devise/models/confirmable.rb', line 130

def skip_confirmation!
  self.confirmed_at = Time.now.utc
end

#skip_confirmation_notification!Object

Skips sending the confirmation notification email after_create. Unlike #skip_confirmation!, record still requires confirmation.



136
137
138
# File 'lib/devise/models/confirmable.rb', line 136

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!



142
143
144
# File 'lib/devise/models/confirmable.rb', line 142

def skip_reconfirmation!
  @bypass_postpone = true
end