Module: Devise::Models::Confirmable

Extended by:
ActiveSupport::Concern
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 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 setup (t.reconfirmable in migrations). Until confirmed new email is
  stored in unconfirmed email column, and copied to email column on successful
  confirmation.

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

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) required_fields(klass)



39
40
41
42
43
# File 'lib/devise/models/confirmable.rb', line 39

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

- (Boolean) active_for_authentication?

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)


93
94
95
# File 'lib/devise/models/confirmable.rb', line 93

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

- (Object) confirm!

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/devise/models/confirmable.rb', line 48

def confirm!
  pending_any_confirmation do
    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

- (Boolean) confirmed?

Verifies whether a user is confirmed or not

Returns:

  • (Boolean)


67
68
69
# File 'lib/devise/models/confirmable.rb', line 67

def confirmed?
  !!confirmed_at
end

- (Object) headers_for(action)



114
115
116
117
118
119
120
# File 'lib/devise/models/confirmable.rb', line 114

def headers_for(action)
  headers = super
  if action == :confirmation_instructions && pending_reconfirmation?
    headers[:to] = unconfirmed_email
  end
  headers
end

- (Object) inactive_message

The message to be shown if the account is inactive.



98
99
100
# File 'lib/devise/models/confirmable.rb', line 98

def inactive_message
  !confirmed? ? :unconfirmed : super
end

- (Boolean) pending_reconfirmation?

Returns:

  • (Boolean)


71
72
73
# File 'lib/devise/models/confirmable.rb', line 71

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

- (Object) resend_confirmation_token

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



85
86
87
# File 'lib/devise/models/confirmable.rb', line 85

def resend_confirmation_token
  pending_any_confirmation { send_confirmation_instructions }
end

- (Object) send_confirmation_instructions

Send confirmation instructions by email



76
77
78
79
80
81
82
# File 'lib/devise/models/confirmable.rb', line 76

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

  generate_confirmation_token! if self.confirmation_token.blank?
  self.devise_mailer.confirmation_instructions(self).deliver
end

- (Object) skip_confirmation!

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



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

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

- (Object) skip_reconfirmation!

If you don't want reconfirmation to be sent, neither a code to be generated, call skip_reconfirmation!



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

def skip_reconfirmation!
  @bypass_postpone = true
end