Module: Sorcery::Model::Submodules::MagicLogin

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

Overview

This submodule adds the ability to login via email without password. 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 login.

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
# File 'lib/sorcery/model/submodules/magic_login.rb', line 13

def self.included(base)
  base.sorcery_config.class_eval do
    attr_accessor :magic_login_token_attribute_name, # magic login code attribute name.
                  :magic_login_token_expires_at_attribute_name, # expires at attribute name.
                  :magic_login_email_sent_at_attribute_name, # when was email sent, used for hammering
                  # protection.
                  :magic_login_mailer_class, # mailer class. Needed.
                  :magic_login_mailer_disabled, # when true sorcery will not automatically
                  # email magic login details and allow you to
                  # manually handle how and when email is sent
                  :magic_login_email_method_name, # magic login email method on your
                  # mailer class.
                  :magic_login_expiration_period, # how many seconds before the request
                  # expires. nil for never expires.
                  :magic_login_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!(:@magic_login_token_attribute_name => :magic_login_token,
                     :@magic_login_token_expires_at_attribute_name => :magic_login_token_expires_at,
                     :@magic_login_email_sent_at_attribute_name => :magic_login_email_sent_at,
                     :@magic_login_mailer_class => nil,
                     :@magic_login_mailer_disabled => true,
                     :@magic_login_email_method_name => :magic_login_email,
                     :@magic_login_expiration_period => 15 * 60,
                     :@magic_login_time_between_emails => 5 * 60)

    reset!
  end

  base.extend(ClassMethods)

  base.sorcery_config.after_config << :validate_mailer_defined
  base.sorcery_config.after_config << :define_magic_login_fields

  base.send(:include, InstanceMethods)
end