module Sorcery::Model::Submodules::ResetPassword

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.

Public Class Methods

included(base) click to toggle source
# File lib/sorcery/model/submodules/reset_password.rb, line 13
def self.included(base)
  base.sorcery_config.class_eval do
    # Reset password code attribute name.
    attr_accessor :reset_password_token_attribute_name
    # Expires at attribute name.
    attr_accessor :reset_password_token_expires_at_attribute_name
    # Counter access to reset password page
    attr_accessor :reset_password_page_access_count_attribute_name
    # When was email sent, used for hammering protection.
    attr_accessor :reset_password_email_sent_at_attribute_name
    # Mailer class (needed)
    attr_accessor :reset_password_mailer
    # When true sorcery will not automatically email password reset details and allow you to
    # manually handle how and when email is sent
    attr_accessor :reset_password_mailer_disabled
    # Reset password email method on your mailer class.
    attr_accessor :reset_password_email_method_name
    # How many seconds before the reset request expires. nil for never expires.
    attr_accessor :reset_password_expiration_period
    # Hammering protection, how long to wait before allowing another email to be sent.
    attr_accessor :reset_password_time_between_emails
  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_page_access_count_attribute_name =>
                         :access_count_to_reset_password_page,
                     :@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