Class: Devise::Passwordless::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/devise/passwordless/install_generator.rb

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_generator_rootObject



10
11
12
# File 'lib/generators/devise/passwordless/install_generator.rb', line 10

def self.default_generator_root
  File.dirname(__FILE__)
end

Instance Method Details

#add_mailer_viewObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/generators/devise/passwordless/install_generator.rb', line 45

def add_mailer_view
  create_file "app/views/devise/mailer/magic_link.html.erb" do <<~'FILE'
    <p>Hello <%= @resource.email %>!</p>

    <p>You can login using the link below:</p>
    
    <p><%= link_to "Log in to my account", magic_link_url(@resource, @scope_name => {email: @resource.email, token: @token, remember_me: @remember_me}) %></p>
    
    <p>Note that the link will expire in <%= Devise.passwordless_login_within.inspect %>.</p>          
  FILE
  end
end

#update_devise_initializerObject



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
# File 'lib/generators/devise/passwordless/install_generator.rb', line 14

def update_devise_initializer
  inject_into_file 'config/initializers/devise.rb', before: /^end$/ do <<~'CONFIG'.indent(2)

    # ==> Configuration for :magic_link_authenticatable

    # Need to use a custom Devise mailer in order to send magic links.
    # If you're already using a custom mailer just have it inherit from
    # Devise::Passwordless::Mailer instead of Devise::Mailer
    config.mailer = "Devise::Passwordless::Mailer"

    # Which algorithm to use for tokenizing magic links. See README for descriptions
    config.passwordless_tokenizer = "SignedGlobalIDTokenizer"

    # Time period after a magic login link is sent out that it will be valid for.
    # config.passwordless_login_within = 20.minutes

    # The secret key used to generate passwordless login tokens. The default value
    # is nil, which means defer to Devise's `secret_key` config value. Changing this
    # key will render invalid all existing passwordless login tokens. You can
    # generate your own secret value with e.g. `rake secret`
    # config.passwordless_secret_key = nil

    # When using the :trackable module and MessageEncryptorTokenizer, set to true to
    # consider magic link tokens generated before the user's current sign in time to
    # be expired. In other words, each time you sign in, all existing magic links
    # will be considered invalid.
    # config.passwordless_expire_old_tokens_on_sign_in = false
  CONFIG
  end
end

#update_devise_yamlObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/generators/devise/passwordless/install_generator.rb', line 58

def update_devise_yaml
  devise_yaml = "config/locales/devise.en.yml"
  existing_config = {}
  begin
    in_root do
      existing_config = YAML.load_file(devise_yaml)
    end
  rescue Errno::ENOENT
    say_status :skip, devise_yaml, :yellow
    return
  end
  default_config = {
    en: {
      devise: {
        passwordless: {
          not_found_in_database: "Could not find a user for that email address",
          magic_link_sent: "A login link has been sent to your email address. Please follow the link to log in to your account.",
          magic_link_sent_paranoid: "If your account exists, you will receive an email with a login link. Please follow the link to log in to your account.",
        },
        failure: {
          magic_link_invalid: "Invalid or expired login link.",
        },
        mailer: {
          magic_link: {
            subject: "Here's your magic login link ✨",
          },
        }
      }
    }
  }
  merged_config = existing_config.deep_merge(default_config.deep_stringify_keys)
  if existing_config.to_yaml == merged_config.to_yaml
    say_status :identical, devise_yaml, :blue
  else
    in_root do
      File.open(devise_yaml, "w") do |f|
        f.write(force_double_quote_yaml(merged_config.to_yaml))
      end
    end
    say_status :insert, devise_yaml, :green
  end
end