authentication-zero 2.11.0 → 2.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/Gemfile.lock +1 -1
  4. data/README.md +3 -1
  5. data/lib/authentication_zero/version.rb +1 -1
  6. data/lib/generators/authentication/USAGE +1 -1
  7. data/lib/generators/authentication/authentication_generator.rb +20 -15
  8. data/lib/generators/authentication/templates/controllers/api/authentications/events_controller.rb.tt +1 -1
  9. data/lib/generators/authentication/templates/controllers/api/identity/email_verifications_controller.rb.tt +16 -6
  10. data/lib/generators/authentication/templates/controllers/api/identity/emails_controller.rb.tt +15 -8
  11. data/lib/generators/authentication/templates/controllers/api/identity/password_resets_controller.rb.tt +9 -9
  12. data/lib/generators/authentication/templates/controllers/api/passwords_controller.rb.tt +8 -8
  13. data/lib/generators/authentication/templates/controllers/api/registrations_controller.rb.tt +5 -5
  14. data/lib/generators/authentication/templates/controllers/api/sessions/sudos_controller.rb.tt +2 -2
  15. data/lib/generators/authentication/templates/controllers/api/sessions_controller.rb.tt +5 -5
  16. data/lib/generators/authentication/templates/controllers/html/authentications/events_controller.rb.tt +1 -1
  17. data/lib/generators/authentication/templates/controllers/html/identity/email_verifications_controller.rb.tt +5 -5
  18. data/lib/generators/authentication/templates/controllers/html/identity/emails_controller.rb.tt +13 -6
  19. data/lib/generators/authentication/templates/controllers/html/identity/password_resets_controller.rb.tt +7 -7
  20. data/lib/generators/authentication/templates/controllers/html/passwords_controller.rb.tt +6 -6
  21. data/lib/generators/authentication/templates/controllers/html/registrations_controller.rb.tt +5 -5
  22. data/lib/generators/authentication/templates/controllers/html/sessions/omniauth_controller.rb.tt +4 -4
  23. data/lib/generators/authentication/templates/controllers/html/sessions/sudos_controller.rb.tt +3 -3
  24. data/lib/generators/authentication/templates/controllers/html/sessions_controller.rb.tt +9 -9
  25. data/lib/generators/authentication/templates/controllers/html/two_factor_authentication/challenges_controller.rb.tt +5 -5
  26. data/lib/generators/authentication/templates/controllers/html/two_factor_authentication/totps_controller.rb.tt +13 -6
  27. data/lib/generators/authentication/templates/erb/authentications/events/{index.html.erb → index.html.erb.tt} +8 -8
  28. data/lib/generators/authentication/templates/erb/identity/emails/edit.html.erb.tt +6 -4
  29. data/lib/generators/authentication/templates/erb/identity/password_resets/edit.html.erb.tt +3 -3
  30. data/lib/generators/authentication/templates/erb/passwords/edit.html.erb.tt +3 -3
  31. data/lib/generators/authentication/templates/erb/registrations/new.html.erb.tt +3 -3
  32. data/lib/generators/authentication/templates/erb/session_mailer/signed_in_notification.html.erb.tt +1 -1
  33. data/lib/generators/authentication/templates/erb/session_mailer/signed_in_notification.text.erb.tt +1 -1
  34. data/lib/generators/authentication/templates/erb/sessions/new.html.erb.tt +3 -2
  35. data/lib/generators/authentication/templates/erb/two_factor_authentication/totps/new.html.erb.tt +2 -0
  36. data/lib/generators/authentication/templates/erb/user_mailer/email_verify_confirmation.html.erb.tt +15 -0
  37. data/lib/generators/authentication/templates/erb/user_mailer/email_verify_confirmation.text.erb.tt +13 -0
  38. data/lib/generators/authentication/templates/erb/{identity_mailer → user_mailer}/password_reset_provision.html.erb.tt +1 -1
  39. data/lib/generators/authentication/templates/erb/{identity_mailer → user_mailer}/password_reset_provision.text.erb.tt +1 -1
  40. data/lib/generators/authentication/templates/mailers/session_mailer.rb.tt +1 -1
  41. data/lib/generators/authentication/templates/mailers/user_mailer.rb.tt +19 -0
  42. data/lib/generators/authentication/templates/migrations/create_events_migration.rb.tt +1 -1
  43. data/lib/generators/authentication/templates/migrations/create_sessions_migration.rb.tt +1 -1
  44. data/lib/generators/authentication/templates/migrations/{create_table_migration.rb.tt → create_users_migration.rb.tt} +3 -3
  45. data/lib/generators/authentication/templates/models/current.rb.tt +2 -2
  46. data/lib/generators/authentication/templates/models/event.rb.tt +1 -1
  47. data/lib/generators/authentication/templates/models/session.rb.tt +5 -5
  48. data/lib/generators/authentication/templates/models/{model.rb.tt → user.rb.tt} +5 -2
  49. data/lib/generators/authentication/templates/test_unit/application_system_test_case.rb.tt +3 -3
  50. data/lib/generators/authentication/templates/test_unit/controllers/api/identity/email_verifications_controller_test.rb.tt +9 -9
  51. data/lib/generators/authentication/templates/test_unit/controllers/api/identity/emails_controller_test.rb.tt +1 -1
  52. data/lib/generators/authentication/templates/test_unit/controllers/api/identity/password_resets_controller_test.rb.tt +7 -7
  53. data/lib/generators/authentication/templates/test_unit/controllers/api/passwords_controller_test.rb.tt +1 -1
  54. data/lib/generators/authentication/templates/test_unit/controllers/api/registrations_controller_test.rb.tt +1 -1
  55. data/lib/generators/authentication/templates/test_unit/controllers/api/sessions_controller_test.rb.tt +6 -6
  56. data/lib/generators/authentication/templates/test_unit/controllers/html/identity/email_verifications_controller_test.rb.tt +9 -9
  57. data/lib/generators/authentication/templates/test_unit/controllers/html/identity/emails_controller_test.rb.tt +1 -1
  58. data/lib/generators/authentication/templates/test_unit/controllers/html/identity/password_resets_controller_test.rb.tt +7 -7
  59. data/lib/generators/authentication/templates/test_unit/controllers/html/passwords_controller_test.rb.tt +1 -1
  60. data/lib/generators/authentication/templates/test_unit/controllers/html/registrations_controller_test.rb.tt +1 -1
  61. data/lib/generators/authentication/templates/test_unit/controllers/html/sessions_controller_test.rb.tt +8 -8
  62. data/lib/generators/authentication/templates/test_unit/system/identity/emails_test.rb.tt +2 -2
  63. data/lib/generators/authentication/templates/test_unit/system/identity/password_resets_test.rb.tt +3 -3
  64. data/lib/generators/authentication/templates/test_unit/system/passwords_test.rb.tt +1 -1
  65. data/lib/generators/authentication/templates/test_unit/system/sessions_test.rb.tt +4 -4
  66. data/lib/generators/authentication/templates/test_unit/test_helper.rb.tt +4 -4
  67. data/lib/generators/authentication/templates/test_unit/{fixtures.yml.tt → users.yml.tt} +0 -0
  68. metadata +11 -11
  69. data/lib/generators/authentication/templates/erb/identity_mailer/email_verify_confirmation.html.erb.tt +0 -11
  70. data/lib/generators/authentication/templates/erb/identity_mailer/email_verify_confirmation.text.erb.tt +0 -9
  71. data/lib/generators/authentication/templates/mailers/identity_mailer.rb.tt +0 -15
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 702b78645aff0919daf1e518101731363068e6aef74fac0591f3257b5bf6b7a3
4
- data.tar.gz: d07d22eb48277537484ef5f5c1cd4fd78f65e65d739fb7031384e3a594248e3a
3
+ metadata.gz: 65673b94c25fddcc0a7bdd31d9dd4a15062dd863b464e712b248b2d4dc2b280e
4
+ data.tar.gz: b3012f74b8981d7f2189bd67c08a098412b48c50b763ec509c45ea19c8e1c4f3
5
5
  SHA512:
6
- metadata.gz: 99224479fcc817abaeed4492a5a48d071e98e3e32fcea32ec56e77007031b186feb0978405e5ec5f90750cf434bc86a554df73dea88245aa6185f18a16d7d2e3
7
- data.tar.gz: 344af675d6c106d41c3a34dc2fdd04d93ca6028c83bc5acda497791a70a220a7854455cb336345d00d9371b529b7581d64617f4c244b1c15182bf5a869ada997
6
+ metadata.gz: dd34f0996158fad54a8514e7ff897db876898e99a00e5ad04181be8747e4886dc27be9b0e2b4b1f965427df05b3ab899920dcf8ffb310c4cb16701d91f28781d
7
+ data.tar.gz: c2ada3dffc843ad15589db3ba23be0dc80c87a110b53d6d24965957d3a736fc8258074da308ffb2bf08de0d59f4b772a4edb639f8bad107be4f98513006ecf22
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## Authentication Zero 2.12.0 (March 28, 2022) ##
2
+
3
+ * Remove model option from generator
4
+
1
5
  ## Authentication Zero 2.11.0 (March 27, 2022) ##
2
6
 
3
7
  * Remove sudo from default generator
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- authentication-zero (2.11.0)
4
+ authentication-zero (2.12.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -13,6 +13,8 @@ The purpose of authentication zero is to generate a pre-built authentication sys
13
13
  - Authentication by token (--api)
14
14
  - Two factor authentication (--two-factor)
15
15
  - Social Login with OmniAuth (--omniauthable)
16
+ - Verify email using a link with token
17
+ - Verify email using a six random digits code for api (--code-verifiable)
16
18
  - Ask password before sensitive data changes, aka: sudo (--sudoable)
17
19
  - Reset the user password and send reset instructions
18
20
  - Reset the user password only from verified emails
@@ -98,7 +100,7 @@ config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
98
100
  ## Usage
99
101
 
100
102
  ```
101
- rails generate authentication user
103
+ rails generate authentication
102
104
  ```
103
105
 
104
106
  Then run `bundle install` again!
@@ -1,3 +1,3 @@
1
1
  module AuthenticationZero
2
- VERSION = "2.11.0"
2
+ VERSION = "2.12.0"
3
3
  end
@@ -8,4 +8,4 @@ Description:
8
8
  to modify the authentication system so it works best with their app.
9
9
 
10
10
  Example:
11
- bin/rails generate authentication user
11
+ bin/rails generate authentication
@@ -1,16 +1,17 @@
1
1
  require "rails/generators/active_record"
2
2
 
3
- class AuthenticationGenerator < Rails::Generators::NamedBase
3
+ class AuthenticationGenerator < Rails::Generators::Base
4
4
  include ActiveRecord::Generators::Migration
5
5
 
6
- class_option :api, type: :boolean, desc: "Generates API authentication"
7
- class_option :pwned, type: :boolean, desc: "Add pwned password validation"
8
- class_option :sudoable, type: :boolean, desc: "Add password request before sensitive data changes"
9
- class_option :lockable, type: :boolean, desc: "Add password reset locking"
10
- class_option :ratelimit, type: :boolean, desc: "Add request rate limiting"
11
- class_option :omniauthable, type: :boolean, desc: "Add social login support"
12
- class_option :trackable, type: :boolean, desc: "Add activity log support"
13
- class_option :two_factor, type: :boolean, desc: "Add two factor authentication"
6
+ class_option :api, type: :boolean, desc: "Generates API authentication"
7
+ class_option :pwned, type: :boolean, desc: "Add pwned password validation"
8
+ class_option :code_verifiable, type: :boolean, desc: "Add email verification using a code for api"
9
+ class_option :sudoable, type: :boolean, desc: "Add password request before sensitive data changes"
10
+ class_option :lockable, type: :boolean, desc: "Add password reset locking"
11
+ class_option :ratelimit, type: :boolean, desc: "Add request rate limiting"
12
+ class_option :omniauthable, type: :boolean, desc: "Add social login support"
13
+ class_option :trackable, type: :boolean, desc: "Add activity log support"
14
+ class_option :two_factor, type: :boolean, desc: "Add two factor authentication"
14
15
 
15
16
  source_root File.expand_path("templates", __dir__)
16
17
 
@@ -53,13 +54,13 @@ class AuthenticationGenerator < Rails::Generators::NamedBase
53
54
  end
54
55
 
55
56
  def create_migrations
56
- migration_template "migrations/create_table_migration.rb", "#{db_migrate_path}/create_#{table_name}.rb"
57
+ migration_template "migrations/create_users_migration.rb", "#{db_migrate_path}/create_users.rb"
57
58
  migration_template "migrations/create_sessions_migration.rb", "#{db_migrate_path}/create_sessions.rb"
58
59
  migration_template "migrations/create_events_migration.rb", "#{db_migrate_path}/create_events.rb" if options.trackable?
59
60
  end
60
61
 
61
62
  def create_models
62
- template "models/model.rb", "app/models/#{file_name}.rb"
63
+ template "models/user.rb", "app/models/user.rb"
63
64
  template "models/session.rb", "app/models/session.rb"
64
65
  template "models/current.rb", "app/models/current.rb"
65
66
  template "models/locking.rb", "app/models/locking.rb" if options.lockable?
@@ -67,7 +68,7 @@ class AuthenticationGenerator < Rails::Generators::NamedBase
67
68
  end
68
69
 
69
70
  def create_fixture_file
70
- template "test_unit/fixtures.yml", "test/fixtures/#{fixture_file_name}.yml"
71
+ template "test_unit/users.yml", "test/fixtures/users.yml"
71
72
  end
72
73
 
73
74
  def create_controllers
@@ -85,10 +86,10 @@ class AuthenticationGenerator < Rails::Generators::NamedBase
85
86
 
86
87
  def create_views
87
88
  if options.api?
88
- directory "erb/identity_mailer", "app/views/identity_mailer"
89
+ directory "erb/user_mailer", "app/views/user_mailer"
89
90
  directory "erb/session_mailer", "app/views/session_mailer"
90
91
  else
91
- directory "erb/identity_mailer", "app/views/identity_mailer"
92
+ directory "erb/user_mailer", "app/views/user_mailer"
92
93
  directory "erb/session_mailer", "app/views/session_mailer"
93
94
 
94
95
  directory "erb/identity", "app/views/identity"
@@ -157,7 +158,11 @@ class AuthenticationGenerator < Rails::Generators::NamedBase
157
158
  options.two_factor? && !options.api?
158
159
  end
159
160
 
161
+ def code_verifiable?
162
+ options.code_verifiable? && options.api?
163
+ end
164
+
160
165
  def redis?
161
- options.lockable? || options.sudoable?
166
+ options.lockable? || options.sudoable? || code_verifiable?
162
167
  end
163
168
  end
@@ -1,5 +1,5 @@
1
1
  class Authentications::EventsController < ApplicationController
2
2
  def index
3
- render json: Current.<%= singular_table_name %>.events.order(created_at: :desc)
3
+ render json: Current.user.events.order(created_at: :desc)
4
4
  end
5
5
  end
@@ -1,20 +1,30 @@
1
1
  class Identity::EmailVerificationsController < ApplicationController
2
2
  skip_before_action :authenticate, only: :edit
3
3
 
4
- before_action :set_<%= singular_table_name %>, only: :edit
4
+ before_action :set_user, only: :edit
5
5
 
6
6
  def edit
7
- @<%= singular_table_name %>.update! verified: true
7
+ @user.update! verified: true
8
8
  end
9
9
 
10
10
  def create
11
- IdentityMailer.with(<%= singular_table_name %>: Current.<%= singular_table_name %>).email_verify_confirmation.deliver_later
11
+ UserMailer.with(user: Current.user).email_verify_confirmation.deliver_later
12
12
  end
13
13
 
14
14
  private
15
- def set_<%= singular_table_name %>
16
- @<%= singular_table_name %> = <%= class_name %>.where(email: params[:email]).find_signed!(params[:token], purpose: params[:email])
15
+ def set_user
16
+ <%- if code_verifiable? -%>
17
+ verified_user = User.find_by(email: params[:email])
18
+
19
+ if verified_user && verified_user.verification_code.value == params[:token]
20
+ @user = verified_user
21
+ else
22
+ render json: { error: "That email verification code is invalid" }, status: :bad_request
23
+ end
24
+ <%- else -%>
25
+ @user = User.where(email: params[:email]).find_signed!(params[:token], purpose: params[:email])
17
26
  rescue
18
27
  render json: { error: "That email verification link is invalid" }, status: :bad_request
19
- end
28
+ <%- end -%>
29
+ end
20
30
  end
@@ -1,22 +1,29 @@
1
1
  class Identity::EmailsController < ApplicationController
2
- before_action :set_<%= singular_table_name %>
2
+ <%- if options.sudoable? -%>
3
+ before_action :require_sudo
4
+ <%- end -%>
5
+ before_action :set_user
3
6
 
4
7
  def update
5
- if !@<%= singular_table_name %>.authenticate(params[:current_password])
8
+ <%- unless options.sudoable? -%>
9
+ if !@user.authenticate(params[:current_password])
6
10
  render json: { error: "The password you entered is incorrect" }, status: :bad_request
7
- elsif @<%= singular_table_name %>.update(<%= "#{singular_table_name}_params" %>)
8
- render json: @<%= singular_table_name %>
11
+ elsif @user.update(user_params)
12
+ <%- else -%>
13
+ if @user.update(user_params)
14
+ <%- end -%>
15
+ render json: @user
9
16
  else
10
- render json: @<%= singular_table_name %>.errors, status: :unprocessable_entity
17
+ render json: @user.errors, status: :unprocessable_entity
11
18
  end
12
19
  end
13
20
 
14
21
  private
15
- def set_<%= singular_table_name %>
16
- @<%= singular_table_name %> = Current.<%= singular_table_name %>
22
+ def set_user
23
+ @user = Current.user
17
24
  end
18
25
 
19
- def <%= "#{singular_table_name}_params" %>
26
+ def user_params
20
27
  params.permit(:email)
21
28
  end
22
29
  end
@@ -4,32 +4,32 @@ class Identity::PasswordResetsController < ApplicationController
4
4
  <%- if options.lockable? -%>
5
5
  before_action :require_locking, only: :create
6
6
  <%- end -%>
7
- before_action :set_<%= singular_table_name %>, only: :update
7
+ before_action :set_user, only: :update
8
8
 
9
9
  def create
10
- if @<%= singular_table_name %> = <%= class_name %>.find_by(email: params[:email], verified: true)
11
- IdentityMailer.with(<%= singular_table_name %>: @<%= singular_table_name %>).password_reset_provision.deliver_later
10
+ if @user = User.find_by(email: params[:email], verified: true)
11
+ UserMailer.with(user: @user).password_reset_provision.deliver_later
12
12
  else
13
13
  render json: { error: "You can't reset your password until you verify your email" }, status: :not_found
14
14
  end
15
15
  end
16
16
 
17
17
  def update
18
- if @<%= singular_table_name %>.update(<%= "#{singular_table_name}_params" %>)
19
- render json: @<%= singular_table_name %>
18
+ if @user.update(user_params)
19
+ render json: @user
20
20
  else
21
- render json: @<%= singular_table_name %>.errors, status: :unprocessable_entity
21
+ render json: @user.errors, status: :unprocessable_entity
22
22
  end
23
23
  end
24
24
 
25
25
  private
26
- def set_<%= singular_table_name %>
27
- @<%= singular_table_name %> = <%= class_name %>.find_signed!(params[:token], purpose: :password_reset)
26
+ def set_user
27
+ @user = User.find_signed!(params[:token], purpose: :password_reset)
28
28
  rescue
29
29
  render json: { error: "That password reset link is invalid" }, status: :bad_request
30
30
  end
31
31
 
32
- def <%= "#{singular_table_name}_params" %>
32
+ def user_params
33
33
  params.permit(:password, :password_confirmation)
34
34
  end
35
35
  <%- if options.lockable? %>
@@ -1,22 +1,22 @@
1
1
  class PasswordsController < ApplicationController
2
- before_action :set_<%= singular_table_name %>
2
+ before_action :set_user
3
3
 
4
4
  def update
5
- if !@<%= singular_table_name %>.authenticate(params[:current_password])
5
+ if !@user.authenticate(params[:current_password])
6
6
  render json: { error: "The current password you entered is incorrect" }, status: :bad_request
7
- elsif @<%= singular_table_name %>.update(<%= "#{singular_table_name}_params" %>)
8
- render json: @<%= singular_table_name %>
7
+ elsif @user.update(user_params)
8
+ render json: @user
9
9
  else
10
- render json: @<%= singular_table_name %>.errors, status: :unprocessable_entity
10
+ render json: @user.errors, status: :unprocessable_entity
11
11
  end
12
12
  end
13
13
 
14
14
  private
15
- def set_<%= singular_table_name %>
16
- @<%= singular_table_name %> = Current.<%= singular_table_name %>
15
+ def set_user
16
+ @user = Current.user
17
17
  end
18
18
 
19
- def <%= "#{singular_table_name}_params" %>
19
+ def user_params
20
20
  params.permit(:password, :password_confirmation)
21
21
  end
22
22
  end
@@ -2,17 +2,17 @@ class RegistrationsController < ApplicationController
2
2
  skip_before_action :authenticate
3
3
 
4
4
  def create
5
- @<%= singular_table_name %> = <%= class_name %>.new(<%= "#{singular_table_name}_params" %>)
5
+ @user = User.new(user_params)
6
6
 
7
- if @<%= singular_table_name %>.save
8
- render json: @<%= singular_table_name %>, status: :created
7
+ if @user.save
8
+ render json: @user, status: :created
9
9
  else
10
- render json: @<%= singular_table_name %>.errors, status: :unprocessable_entity
10
+ render json: @user.errors, status: :unprocessable_entity
11
11
  end
12
12
  end
13
13
 
14
14
  private
15
- def <%= "#{singular_table_name}_params" %>
15
+ def user_params
16
16
  params.permit(:email, :password, :password_confirmation)
17
17
  end
18
18
  end
@@ -2,8 +2,8 @@ class Sessions::SudosController < ApplicationController
2
2
  def create
3
3
  session = Current.session
4
4
 
5
- if session.<%= singular_table_name %>.authenticate(params[:password])
6
- session.sudo.mark expires_in: 30.minutes
5
+ if session.user.authenticate(params[:password])
6
+ session.sudo.mark
7
7
  else
8
8
  render json: { error: "The password you entered is incorrect" }, status: :bad_request
9
9
  end
@@ -4,7 +4,7 @@ class SessionsController < ApplicationController
4
4
  before_action :set_session, only: %i[ show destroy ]
5
5
 
6
6
  def index
7
- render json: Current.<%= singular_table_name %>.sessions.order(created_at: :desc)
7
+ render json: Current.user.sessions.order(created_at: :desc)
8
8
  end
9
9
 
10
10
  def show
@@ -12,10 +12,10 @@ class SessionsController < ApplicationController
12
12
  end
13
13
 
14
14
  def create
15
- <%= singular_table_name %> = <%= class_name %>.find_by(email: params[:email])
15
+ user = User.find_by(email: params[:email])
16
16
 
17
- if <%= singular_table_name %> && <%= singular_table_name %>.authenticate(params[:password])
18
- @session = <%= singular_table_name %>.sessions.create!
17
+ if user && user.authenticate(params[:password])
18
+ @session = user.sessions.create!
19
19
  response.set_header("X-Session-Token", @session.signed_id)
20
20
 
21
21
  render json: @session, status: :created
@@ -30,6 +30,6 @@ class SessionsController < ApplicationController
30
30
 
31
31
  private
32
32
  def set_session
33
- @session = Current.<%= singular_table_name %>.sessions.find(params[:id])
33
+ @session = Current.user.sessions.find(params[:id])
34
34
  end
35
35
  end
@@ -1,5 +1,5 @@
1
1
  class Authentications::EventsController < ApplicationController
2
2
  def index
3
- @events = Current.<%= singular_table_name %>.events.order(created_at: :desc)
3
+ @events = Current.user.events.order(created_at: :desc)
4
4
  end
5
5
  end
@@ -1,21 +1,21 @@
1
1
  class Identity::EmailVerificationsController < ApplicationController
2
2
  skip_before_action :authenticate, only: :edit
3
3
 
4
- before_action :set_<%= singular_table_name %>, only: :edit
4
+ before_action :set_user, only: :edit
5
5
 
6
6
  def edit
7
- @<%= singular_table_name %>.update! verified: true
7
+ @user.update! verified: true
8
8
  redirect_to root_path, notice: "Thank you for verifying your email address"
9
9
  end
10
10
 
11
11
  def create
12
- IdentityMailer.with(<%= singular_table_name %>: Current.<%= singular_table_name %>).email_verify_confirmation.deliver_later
12
+ UserMailer.with(user: Current.user).email_verify_confirmation.deliver_later
13
13
  redirect_to root_path, notice: "We sent a verification email to your email address"
14
14
  end
15
15
 
16
16
  private
17
- def set_<%= singular_table_name %>
18
- @<%= singular_table_name %> = <%= class_name %>.where(email: params[:email]).find_signed!(params[:token], purpose: params[:email])
17
+ def set_user
18
+ @user = User.where(email: params[:email]).find_signed!(params[:token], purpose: params[:email])
19
19
  rescue
20
20
  redirect_to edit_identity_email_path, alert: "That email verification link is invalid"
21
21
  end
@@ -1,13 +1,20 @@
1
1
  class Identity::EmailsController < ApplicationController
2
- before_action :set_<%= singular_table_name %>
2
+ <%- if options.sudoable? -%>
3
+ before_action :require_sudo
4
+ <%- end -%>
5
+ before_action :set_user
3
6
 
4
7
  def edit
5
8
  end
6
9
 
7
10
  def update
8
- if !@<%= singular_table_name %>.authenticate(params[:current_password])
11
+ <%- unless options.sudoable? -%>
12
+ if !@user.authenticate(params[:current_password])
9
13
  redirect_to edit_identity_email_path, alert: "The password you entered is incorrect"
10
- elsif @<%= singular_table_name %>.update(<%= "#{singular_table_name}_params" %>)
14
+ elsif @user.update(user_params)
15
+ <%- else -%>
16
+ if @user.update(user_params)
17
+ <%- end -%>
11
18
  redirect_to root_path, notice: "Your email has been changed"
12
19
  else
13
20
  render :edit, status: :unprocessable_entity
@@ -15,11 +22,11 @@ class Identity::EmailsController < ApplicationController
15
22
  end
16
23
 
17
24
  private
18
- def set_<%= singular_table_name %>
19
- @<%= singular_table_name %> = Current.<%= singular_table_name %>
25
+ def set_user
26
+ @user = Current.user
20
27
  end
21
28
 
22
- def <%= "#{singular_table_name}_params" %>
29
+ def user_params
23
30
  params.permit(:email)
24
31
  end
25
32
  end
@@ -4,7 +4,7 @@ class Identity::PasswordResetsController < ApplicationController
4
4
  <%- if options.lockable? -%>
5
5
  before_action :require_locking, only: :create
6
6
  <%- end -%>
7
- before_action :set_<%= singular_table_name %>, only: %i[ edit update ]
7
+ before_action :set_user, only: %i[ edit update ]
8
8
 
9
9
  def new
10
10
  end
@@ -13,8 +13,8 @@ class Identity::PasswordResetsController < ApplicationController
13
13
  end
14
14
 
15
15
  def create
16
- if @<%= singular_table_name %> = <%= class_name %>.find_by(email: params[:email], verified: true)
17
- IdentityMailer.with(<%= singular_table_name %>: @<%= singular_table_name %>).password_reset_provision.deliver_later
16
+ if @user = User.find_by(email: params[:email], verified: true)
17
+ UserMailer.with(user: @user).password_reset_provision.deliver_later
18
18
  redirect_to sign_in_path, notice: "Check your email for reset instructions"
19
19
  else
20
20
  redirect_to new_identity_password_reset_path, alert: "You can't reset your password until you verify your email"
@@ -22,7 +22,7 @@ class Identity::PasswordResetsController < ApplicationController
22
22
  end
23
23
 
24
24
  def update
25
- if @<%= singular_table_name %>.update(<%= "#{singular_table_name}_params" %>)
25
+ if @user.update(user_params)
26
26
  redirect_to sign_in_path, notice: "Your password was reset successfully. Please sign in"
27
27
  else
28
28
  render :edit, status: :unprocessable_entity
@@ -30,13 +30,13 @@ class Identity::PasswordResetsController < ApplicationController
30
30
  end
31
31
 
32
32
  private
33
- def set_<%= singular_table_name %>
34
- @<%= singular_table_name %> = <%= class_name %>.find_signed!(params[:token], purpose: :password_reset)
33
+ def set_user
34
+ @user = User.find_signed!(params[:token], purpose: :password_reset)
35
35
  rescue
36
36
  redirect_to new_identity_password_reset_path, alert: "That password reset link is invalid"
37
37
  end
38
38
 
39
- def <%= "#{singular_table_name}_params" %>
39
+ def user_params
40
40
  params.permit(:password, :password_confirmation)
41
41
  end
42
42
  <%- if options.lockable? %>
@@ -1,13 +1,13 @@
1
1
  class PasswordsController < ApplicationController
2
- before_action :set_<%= singular_table_name %>
2
+ before_action :set_user
3
3
 
4
4
  def edit
5
5
  end
6
6
 
7
7
  def update
8
- if !@<%= singular_table_name %>.authenticate(params[:current_password])
8
+ if !@user.authenticate(params[:current_password])
9
9
  redirect_to edit_password_path, alert: "The current password you entered is incorrect"
10
- elsif @<%= singular_table_name %>.update(<%= "#{singular_table_name}_params" %>)
10
+ elsif @user.update(user_params)
11
11
  redirect_to root_path, notice: "Your password has been changed"
12
12
  else
13
13
  render :edit, status: :unprocessable_entity
@@ -15,11 +15,11 @@ class PasswordsController < ApplicationController
15
15
  end
16
16
 
17
17
  private
18
- def set_<%= singular_table_name %>
19
- @<%= singular_table_name %> = Current.<%= singular_table_name %>
18
+ def set_user
19
+ @user = Current.user
20
20
  end
21
21
 
22
- def <%= "#{singular_table_name}_params" %>
22
+ def user_params
23
23
  params.permit(:password, :password_confirmation)
24
24
  end
25
25
  end
@@ -2,14 +2,14 @@ class RegistrationsController < ApplicationController
2
2
  skip_before_action :authenticate
3
3
 
4
4
  def new
5
- @<%= singular_table_name %> = <%= class_name %>.new
5
+ @user = User.new
6
6
  end
7
7
 
8
8
  def create
9
- @<%= singular_table_name %> = <%= class_name %>.new(<%= "#{singular_table_name}_params" %>)
9
+ @user = User.new(user_params)
10
10
 
11
- if @<%= singular_table_name %>.save
12
- session = @<%= singular_table_name %>.sessions.create!
11
+ if @user.save
12
+ session = @user.sessions.create!
13
13
  cookies.signed.permanent[:session_token] = { value: session.id, httponly: true }
14
14
 
15
15
  redirect_to root_path, notice: "Welcome! You have signed up successfully"
@@ -19,7 +19,7 @@ class RegistrationsController < ApplicationController
19
19
  end
20
20
 
21
21
  private
22
- def <%= "#{singular_table_name}_params" %>
22
+ def user_params
23
23
  params.permit(:email, :password, :password_confirmation)
24
24
  end
25
25
  end
@@ -3,10 +3,10 @@ class Sessions::OmniauthController < ApplicationController
3
3
  skip_before_action :authenticate
4
4
 
5
5
  def create
6
- @<%= singular_table_name %> = <%= class_name %>.where(omniauth_params).first_or_initialize(<%= "#{singular_table_name}_params" %>)
6
+ @user = User.where(omniauth_params).first_or_initialize(user_params)
7
7
 
8
- if @<%= singular_table_name %>.save
9
- session = @<%= singular_table_name %>.sessions.create!
8
+ if @user.save
9
+ session = @user.sessions.create!
10
10
  cookies.signed.permanent[:session_token] = { value: session.id, httponly: true }
11
11
 
12
12
  redirect_to root_path, notice: "Signed in successfully"
@@ -24,7 +24,7 @@ class Sessions::OmniauthController < ApplicationController
24
24
  { provider: omniauth.provider, uid: omniauth.uid }
25
25
  end
26
26
 
27
- def <%= "#{singular_table_name}_params" %>
27
+ def user_params
28
28
  { email: omniauth.info.email, password: SecureRandom::base58, verified: true }
29
29
  end
30
30
 
@@ -6,11 +6,11 @@ class Sessions::SudosController < ApplicationController
6
6
  session = Current.session
7
7
 
8
8
  <%- if omniauthable? -%>
9
- if session.<%= singular_table_name %>.authenticate(params[:password]) || session.<%= singular_table_name %>.provider
9
+ if session.user.authenticate(params[:password]) || session.user.provider
10
10
  <%- else -%>
11
- if session.<%= singular_table_name %>.authenticate(params[:password])
11
+ if session.user.authenticate(params[:password])
12
12
  <%- end -%>
13
- session.sudo.mark(expires_in: 30.minutes); redirect_to(params[:proceed_to_url])
13
+ session.sudo.mark; redirect_to(params[:proceed_to_url])
14
14
  else
15
15
  redirect_to new_sessions_sudo_path(proceed_to_url: params[:proceed_to_url]), alert: "The password you entered is incorrect"
16
16
  end
@@ -4,30 +4,30 @@ class SessionsController < ApplicationController
4
4
  before_action :set_session, only: :destroy
5
5
 
6
6
  def index
7
- @sessions = Current.<%= singular_table_name %>.sessions.order(created_at: :desc)
7
+ @sessions = Current.user.sessions.order(created_at: :desc)
8
8
  end
9
9
 
10
10
  def new
11
- @<%= singular_table_name %> = <%= class_name %>.new
11
+ @user = User.new
12
12
  end
13
13
 
14
14
  def create
15
- <%= singular_table_name %> = <%= class_name %>.find_by(email: params[:email])
15
+ user = User.find_by(email: params[:email])
16
16
 
17
- if <%= singular_table_name %> && <%= singular_table_name %>.authenticate(params[:password])
17
+ if user && user.authenticate(params[:password])
18
18
  <%- if two_factor? -%>
19
- if <%= singular_table_name %>.otp_secret
20
- signed_id = <%= singular_table_name %>.signed_id(purpose: :authentication_challenge, expires_in: 20.minutes)
19
+ if user.otp_secret
20
+ signed_id = user.signed_id(purpose: :authentication_challenge, expires_in: 20.minutes)
21
21
 
22
22
  redirect_to new_two_factor_authentication_challenge_path(token: signed_id)
23
23
  else
24
- @session = <%= singular_table_name %>.sessions.create!
24
+ @session = user.sessions.create!
25
25
  cookies.signed.permanent[:session_token] = { value: @session.id, httponly: true }
26
26
 
27
27
  redirect_to root_path, notice: "Signed in successfully"
28
28
  end
29
29
  <%- else -%>
30
- @session = <%= singular_table_name %>.sessions.create!
30
+ @session = user.sessions.create!
31
31
  cookies.signed.permanent[:session_token] = { value: @session.id, httponly: true }
32
32
 
33
33
  redirect_to root_path, notice: "Signed in successfully"
@@ -43,6 +43,6 @@ class SessionsController < ApplicationController
43
43
 
44
44
  private
45
45
  def set_session
46
- @session = Current.<%= singular_table_name %>.sessions.find(params[:id])
46
+ @session = Current.user.sessions.find(params[:id])
47
47
  end
48
48
  end