Class: RailsAuthentication::Generators::AuthenticationGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
ActiveRecord::Generators::Migration, Features::Confirmable, Features::Invitable, Features::Lockable, Features::MagicLink, Features::Ott, Features::Passkey, Features::Recoverable, Features::Registerable, Features::Rememberable, Features::Timeoutable, Features::Trackable, Features::Validatable
Defined in:
lib/generators/authentication/authentication_generator.rb

Overview

Shadows Rails' built-in bin/rails generate authentication command: the explicit authentication:authentication namespace is checked before Rails' own rails:authentication, so the bare authentication invocation lands here. It runs the built-in generator first, then layers the feature set on top.

The Ruby namespace is RailsAuthentication (not Authentication, which would give the same Thor namespace implicitly) because Rails 8's base generator creates an Authentication controller concern in the host app — a top-level constant this gem must not squat on.

Constant Summary collapse

FEATURES =
i[
  confirmable recoverable registerable rememberable trackable
  timeoutable validatable lockable invitable
].freeze

Instance Method Summary collapse

Instance Method Details

#customize_session_layerObject

Confirmable, Rememberable, Trackable, Timeoutable, and Lockable all hook into the sign-in flow, so the base generator's session files are replaced with versions rendered from the enabled feature set. Overwriting is safe: the base copies were written moments ago by this same run.



142
143
144
145
146
# File 'lib/generators/authentication/authentication_generator.rb', line 142

def customize_session_layer
  template "app/controllers/sessions_controller.rb", force: true
  template "app/controllers/concerns/authentication.rb", force: true
  template "app/views/sessions/new.html.erb", force: true
end

#format_user_modelObject

Runs after every feature install so the blank line separates the concern includes (if any) from the rest of the class body, no matter which features are enabled.



134
135
136
# File 'lib/generators/authentication/authentication_generator.rb', line 134

def format_user_model
  inject_into_file "app/models/user.rb", "\n", after: /(?:  include \w+Concern\n)+/, force: true
end

#install_base_authenticationObject



78
79
80
81
# File 'lib/generators/authentication/authentication_generator.rb', line 78

def install_base_authentication
  say "Running Rails' built-in authentication generator", :green
  Rails::Generators.invoke("rails:authentication", [], behavior: behavior, destination_root: destination_root)
end

#install_confirmableObject



95
96
97
# File 'lib/generators/authentication/authentication_generator.rb', line 95

def install_confirmable
  generate_confirmable if confirmable?
end

#install_invitableObject



115
116
117
# File 'lib/generators/authentication/authentication_generator.rb', line 115

def install_invitable
  generate_invitable if invitable?
end

#install_lockableObject



111
112
113
# File 'lib/generators/authentication/authentication_generator.rb', line 111

def install_lockable
  generate_lockable if lockable?
end


119
120
121
# File 'lib/generators/authentication/authentication_generator.rb', line 119

def install_magic_link
  generate_magic_link if magic_link?
end

#install_ottObject



123
124
125
# File 'lib/generators/authentication/authentication_generator.rb', line 123

def install_ott
  generate_ott if ott?
end

#install_passkeyObject



127
128
129
# File 'lib/generators/authentication/authentication_generator.rb', line 127

def install_passkey
  generate_passkey if passkey?
end

#install_recoverableObject



91
92
93
# File 'lib/generators/authentication/authentication_generator.rb', line 91

def install_recoverable
  generate_recoverable if recoverable?
end

#install_registerableObject



87
88
89
# File 'lib/generators/authentication/authentication_generator.rb', line 87

def install_registerable
  generate_registerable if registerable?
end

#install_rememberableObject



99
100
101
# File 'lib/generators/authentication/authentication_generator.rb', line 99

def install_rememberable
  generate_rememberable if rememberable?
end

#install_timeoutableObject



107
108
109
# File 'lib/generators/authentication/authentication_generator.rb', line 107

def install_timeoutable
  generate_timeoutable if timeoutable?
end

#install_trackableObject



103
104
105
# File 'lib/generators/authentication/authentication_generator.rb', line 103

def install_trackable
  generate_trackable if trackable?
end

#install_validatableObject



83
84
85
# File 'lib/generators/authentication/authentication_generator.rb', line 83

def install_validatable
  generate_validatable if validatable?
end