Module: Sorcery::Model::Submodules::ResetPassword

Defined in:
lib/sorcery/model/submodules/reset_password.rb

Overview

This submodule adds the ability to reset password via email confirmation. When the user requests an email is sent to him with a url. The url includes a token, which is also saved with the user’s record in the db. The token has configurable expiration. When the user clicks the url in the email, providing the token has not yet expired, he will be able to reset his password via a form.

When using this submodule, supplying a mailer is mandatory.

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sorcery/model/submodules/reset_password.rb', line 13

def self.included(base)
  base.sorcery_config.class_eval do
    attr_accessor :reset_password_token_attribute_name,              # reset password code attribute name.
                  :reset_password_token_expires_at_attribute_name,   # expires at attribute name.
                  :reset_password_email_sent_at_attribute_name,      # when was email sent, used for hammering
                                                                     # protection.

                  :reset_password_mailer,                            # mailer class. Needed.

                  :reset_password_mailer_disabled,                   # when true sorcery will not automatically
                                                                     # email password reset details and allow you to
                                                                     # manually handle how and when email is sent

                  :reset_password_email_method_name,                 # reset password email method on your
                                                                     # mailer class.

                  :reset_password_expiration_period,                 # how many seconds before the reset request
                                                                     # expires. nil for never expires.

                  :reset_password_time_between_emails                # hammering protection, how long to wait
                                                                     # before allowing another email to be sent.

  end

  base.sorcery_config.instance_eval do
    @defaults.merge!(:@reset_password_token_attribute_name            => :reset_password_token,
                     :@reset_password_token_expires_at_attribute_name => :reset_password_token_expires_at,
                     :@reset_password_email_sent_at_attribute_name    => :reset_password_email_sent_at,
                     :@reset_password_mailer                          => nil,
                     :@reset_password_mailer_disabled                 => false,
                     :@reset_password_email_method_name               => :reset_password_email,
                     :@reset_password_expiration_period               => nil,
                     :@reset_password_time_between_emails             => 5 * 60 )

    reset!
  end

  base.extend(ClassMethods)

  base.sorcery_config.after_config << :validate_mailer_defined
  base.sorcery_config.after_config << :define_reset_password_fields

  base.send(:include, InstanceMethods)

end