rails_authentication 0.6.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +100 -10
  3. data/lib/generators/authentication/authentication_generator.rb +135 -8
  4. data/lib/generators/authentication/features/confirmable.rb +5 -5
  5. data/lib/generators/authentication/features/invitable.rb +5 -5
  6. data/lib/generators/authentication/features/lockable.rb +5 -5
  7. data/lib/generators/authentication/features/magic_link.rb +5 -5
  8. data/lib/generators/authentication/features/ott.rb +5 -5
  9. data/lib/generators/authentication/features/recoverable.rb +8 -6
  10. data/lib/generators/authentication/features/rememberable.rb +8 -3
  11. data/lib/generators/authentication/templates/app/controllers/concerns/authentication.rb.tt +67 -15
  12. data/lib/generators/authentication/templates/app/controllers/confirmations_controller.rb.tt +11 -7
  13. data/lib/generators/authentication/templates/app/controllers/invitations_controller.rb.tt +14 -5
  14. data/lib/generators/authentication/templates/app/controllers/magic_links_controller.rb.tt +10 -8
  15. data/lib/generators/authentication/templates/app/controllers/otts_controller.rb.tt +18 -15
  16. data/lib/generators/authentication/templates/app/controllers/passkey_sessions_controller.rb.tt +9 -7
  17. data/lib/generators/authentication/templates/app/controllers/passwords_controller.rb.tt +22 -9
  18. data/lib/generators/authentication/templates/app/controllers/registrations_controller.rb.tt +8 -6
  19. data/lib/generators/authentication/templates/app/controllers/sessions_controller.rb.tt +11 -9
  20. data/lib/generators/authentication/templates/app/controllers/unlocks_controller.rb.tt +8 -6
  21. data/lib/generators/authentication/templates/app/controllers/webauthn_credentials_controller.rb.tt +8 -4
  22. data/lib/generators/authentication/templates/app/mailers/confirmations_mailer.rb.tt +4 -2
  23. data/lib/generators/authentication/templates/app/mailers/invitations_mailer.rb.tt +3 -1
  24. data/lib/generators/authentication/templates/app/mailers/magic_links_mailer.rb.tt +3 -1
  25. data/lib/generators/authentication/templates/app/mailers/otts_mailer.rb.tt +3 -1
  26. data/lib/generators/authentication/templates/app/mailers/passwords_mailer.rb.tt +3 -1
  27. data/lib/generators/authentication/templates/app/mailers/unlocks_mailer.rb.tt +3 -1
  28. data/lib/generators/authentication/templates/app/models/concerns/confirmable_concern.rb.tt +16 -11
  29. data/lib/generators/authentication/templates/app/models/concerns/invitable_concern.rb.tt +13 -2
  30. data/lib/generators/authentication/templates/app/models/concerns/lockable_concern.rb.tt +8 -0
  31. data/lib/generators/authentication/templates/app/models/concerns/magic_link_concern.rb.tt +2 -0
  32. data/lib/generators/authentication/templates/app/models/concerns/ott_concern.rb.tt +2 -0
  33. data/lib/generators/authentication/templates/app/models/concerns/passkey_concern.rb.tt +2 -0
  34. data/lib/generators/authentication/templates/app/models/concerns/recoverable_concern.rb.tt +8 -0
  35. data/lib/generators/authentication/templates/app/models/concerns/timeoutable_concern.rb.tt +2 -0
  36. data/lib/generators/authentication/templates/app/models/concerns/trackable_concern.rb.tt +2 -0
  37. data/lib/generators/authentication/templates/app/models/concerns/validatable_concern.rb.tt +6 -4
  38. data/lib/generators/authentication/templates/app/models/session.rb.tt +45 -0
  39. data/lib/generators/authentication/templates/app/models/user_auth.rb.tt +5 -3
  40. data/lib/generators/authentication/templates/app/models/webauthn_credential.rb.tt +2 -0
  41. data/lib/generators/authentication/templates/app/views/confirmations/new.html.erb.tt +3 -3
  42. data/lib/generators/authentication/templates/app/views/confirmations_mailer/confirmation_instructions.html.erb.tt +3 -3
  43. data/lib/generators/authentication/templates/app/views/confirmations_mailer/confirmation_instructions.text.erb.tt +2 -2
  44. data/lib/generators/authentication/templates/app/views/invitations/edit.html.erb.tt +4 -4
  45. data/lib/generators/authentication/templates/app/views/invitations/new.html.erb.tt +3 -3
  46. data/lib/generators/authentication/templates/app/views/invitations_mailer/invitation_instructions.html.erb.tt +3 -3
  47. data/lib/generators/authentication/templates/app/views/invitations_mailer/invitation_instructions.text.erb.tt +2 -2
  48. data/lib/generators/authentication/templates/app/views/magic_links/new.html.erb.tt +4 -4
  49. data/lib/generators/authentication/templates/app/views/magic_links_mailer/magic_link.html.erb.tt +3 -3
  50. data/lib/generators/authentication/templates/app/views/magic_links_mailer/magic_link.text.erb.tt +2 -3
  51. data/lib/generators/authentication/templates/app/views/otts/edit.html.erb.tt +7 -7
  52. data/lib/generators/authentication/templates/app/views/otts_mailer/ott.html.erb.tt +3 -3
  53. data/lib/generators/authentication/templates/app/views/otts_mailer/ott.text.erb.tt +3 -3
  54. data/lib/generators/authentication/templates/app/views/passwords/edit.html.erb.tt +9 -0
  55. data/lib/generators/authentication/templates/app/views/passwords/new.html.erb.tt +8 -0
  56. data/lib/generators/authentication/templates/app/views/passwords_mailer/reset.html.erb.tt +3 -3
  57. data/lib/generators/authentication/templates/app/views/passwords_mailer/reset.text.erb.tt +2 -2
  58. data/lib/generators/authentication/templates/app/views/registrations/edit.html.erb.tt +7 -7
  59. data/lib/generators/authentication/templates/app/views/registrations/new.html.erb.tt +6 -6
  60. data/lib/generators/authentication/templates/app/views/sessions/new.html.erb.tt +20 -16
  61. data/lib/generators/authentication/templates/app/views/unlocks/new.html.erb.tt +3 -3
  62. data/lib/generators/authentication/templates/app/views/unlocks_mailer/unlock_instructions.html.erb.tt +3 -3
  63. data/lib/generators/authentication/templates/app/views/unlocks_mailer/unlock_instructions.text.erb.tt +3 -3
  64. data/lib/generators/authentication/templates/app/views/webauthn_credentials/index.html.erb.tt +5 -5
  65. data/lib/generators/authentication/templates/app/views/webauthn_credentials/new.html.erb.tt +12 -6
  66. data/lib/generators/authentication/templates/config/initializers/webauthn.rb.tt +2 -0
  67. data/lib/generators/authentication/templates/config/locales/authentication.en.yml.tt +421 -0
  68. data/lib/generators/authentication/templates/db/migrate/add_confirmable_to_users.rb.tt +1 -1
  69. data/lib/generators/authentication/templates/db/migrate/add_rememberable_to_sessions.rb.tt +5 -0
  70. data/lib/generators/authentication/templates/db/migrate/remove_remember_created_at_from_users.rb.tt +11 -0
  71. data/lib/rails_authentication/version.rb +1 -1
  72. metadata +8 -3
  73. data/lib/generators/authentication/templates/app/models/concerns/rememberable_concern.rb.tt +0 -15
  74. data/lib/generators/authentication/templates/db/migrate/add_rememberable_to_users.rb.tt +0 -5
@@ -1,15 +1,34 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Authentication
2
4
  extend ActiveSupport::Concern
3
5
 
4
6
  included do
5
7
  before_action :require_authentication
6
- helper_method :authenticated?, :current_user
8
+ helper_method :authenticated?, :current_user, :authentication_email_column_name
7
9
  end
8
10
 
11
+ # Every controller requires a signed-in user by default (the before_action above).
9
12
  class_methods do
13
+ # Open to everyone, signed in or not.
10
14
  def allow_unauthenticated_access(**options)
11
15
  skip_before_action :require_authentication, **options
12
16
  end
17
+
18
+ # Signed-out only, for sign-in, sign-up, and the other signed-out flows: open to visitors,
19
+ # while a user who is already signed in is sent to the root path instead.
20
+ def require_unauthenticated_access(**options)
21
+ allow_unauthenticated_access(**options)
22
+ before_action :redirect_signed_in_user, **options
23
+ end
24
+
25
+ # Signed-in only. Already the default, so this is for being explicit, or for re-protecting
26
+ # actions a parent controller opened with allow_unauthenticated_access. It registers its
27
+ # own callback rather than :require_authentication: re-registering that one with only:/except:
28
+ # would replace the default and leave the other actions unprotected.
29
+ def require_authenticated_access(**options)
30
+ before_action :enforce_authentication, **options
31
+ end
13
32
  end
14
33
 
15
34
  private
@@ -22,24 +41,40 @@ module Authentication
22
41
  Current.user
23
42
  end
24
43
 
44
+ # The users table's email column, for params, lookups, and form fields. Defined on User.
45
+ def authentication_email_column_name
46
+ User.authentication_email_column_name
47
+ end
48
+
25
49
  def require_authentication
26
50
  resume_session || request_authentication
27
51
  end
28
52
 
53
+ def enforce_authentication
54
+ require_authentication
55
+ end
56
+
57
+ # allow_unauthenticated_access skips resume_session, so authenticated? loads it here.
58
+ def redirect_signed_in_user
59
+ return unless authenticated?
60
+
61
+ if request.format.json?
62
+ render json: { error: t("flash.authentication.alert.already_signed_in") }, status: :forbidden
63
+ else
64
+ redirect_to default_after_authentication_url, alert: t("flash.authentication.alert.already_signed_in")
65
+ end
66
+ end
67
+
29
68
  def resume_session
30
69
  Current.session ||= find_session_by_cookie
31
70
  end
32
71
 
33
- <% if timeoutable? -%>
72
+ <% if timeoutable? || rememberable? -%>
34
73
  def find_session_by_cookie
35
74
  return unless cookies.signed[:session_id]
36
75
  return unless session = Session.find_by(id: cookies.signed[:session_id])
37
76
 
38
- <% if rememberable? -%>
39
- if !session.user.remembered? && session.user.timedout?(session.updated_at)
40
- <% else -%>
41
- if session.user.timedout?(session.updated_at)
42
- <% end -%>
77
+ if session.expired?
43
78
  session.destroy
44
79
  cookies.delete(:session_id)
45
80
  nil
@@ -54,25 +89,36 @@ module Authentication
54
89
  end
55
90
  <% end -%>
56
91
 
92
+ # Only page visits are remembered: returning to a form submission or a JSON/XHR endpoint
93
+ # after sign-in would hit the wrong action (a GET to a POST-only URL) or render raw data.
57
94
  def request_authentication
58
- session[:return_to_after_authenticating] = request.url
95
+ session[:return_to_after_authenticating] = request.url if request.get? && request.format.html? && !request.xhr?
59
96
  redirect_to new_session_path
60
97
  end
61
98
 
99
+ # Where every sign-in flow lands: the page the user was sent away from, otherwise the default.
62
100
  def after_authentication_url
63
- session.delete(:return_to_after_authenticating) || root_url
101
+ session.delete(:return_to_after_authenticating) || default_after_authentication_url
102
+ end
103
+
104
+ # Where signed-in users land when there's no page to return to. Override it in
105
+ # ApplicationController (e.g. `dashboard_url`) to change the default for every flow.
106
+ def default_after_authentication_url
107
+ root_url
64
108
  end
65
109
 
66
110
  <% if rememberable? -%>
111
+ # Remembering is per browser: it's stamped on this session only, never on the user,
112
+ # so other devices' sessions are unaffected.
67
113
  def start_new_session_for(user, remember: false)
68
- user.sessions.create!(user_agent: request.user_agent, ip_address: request.remote_ip).tap do |session|
114
+ user.sessions.create!(
115
+ user_agent: request.user_agent, ip_address: request.remote_ip, remembered_at: (Time.current if remember)
116
+ ).tap do |session|
69
117
  Current.session = session
70
118
 
71
119
  if remember
72
- user.remember_me!
73
120
  cookies.signed.permanent[:session_id] = { value: session.id, httponly: true, same_site: :lax }
74
121
  else
75
- user.forget_me!
76
122
  cookies.signed[:session_id] = { value: session.id, httponly: true, same_site: :lax }
77
123
  end
78
124
  end
@@ -87,10 +133,16 @@ module Authentication
87
133
  <% end -%>
88
134
 
89
135
  def terminate_session
90
- <% if rememberable? -%>
91
- Current.session.user.forget_me!
92
- <% end -%>
93
136
  Current.session.destroy
94
137
  cookies.delete(:session_id)
95
138
  end
139
+
140
+ def terminate_all_sessions_for(user)
141
+ user.sessions.destroy_all
142
+
143
+ if Current.user == user
144
+ Current.session = nil
145
+ cookies.delete(:session_id)
146
+ end
147
+ end
96
148
  end
@@ -1,27 +1,31 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ConfirmationsController < ApplicationController
2
- allow_unauthenticated_access
3
- rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_confirmation_path, alert: "Try again later." }
4
+ # show stays open to everyone: with --reconfirmable, a signed-in user confirms an email change there.
5
+ allow_unauthenticated_access only: :show
6
+ require_unauthenticated_access except: :show
7
+ rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_confirmation_path, alert: t("flash.confirmations.create.alert.rate_limited") }
4
8
 
5
9
  def new
6
10
  end
7
11
 
8
12
  def create
9
- user = User.find_by(email_address: params[:email_address])
13
+ user = User.find_by(authentication_email_column_name => params[authentication_email_column_name])
10
14
  <% if reconfirmable? -%>
11
- user.send_confirmation_instructions if user && (!user.confirmed? || user.unconfirmed_email.present?)
15
+ user.send_confirmation_instructions if user && (!user.confirmed? || user.<%= unconfirmed_email_attribute %>.present?)
12
16
  <% else -%>
13
17
  user.send_confirmation_instructions if user && !user.confirmed?
14
18
  <% end -%>
15
19
 
16
- redirect_to new_session_path, notice: "Confirmation instructions sent (if user with that email address exists and is unconfirmed)."
20
+ redirect_to new_session_path, notice: t("flash.confirmations.create.notice")
17
21
  end
18
22
 
19
23
  def show
20
24
  if user = User.find_by_valid_confirmation_token(params[:token])
21
25
  user.confirm!
22
- redirect_to new_session_path, notice: "Your email address has been confirmed. Please sign in."
26
+ redirect_to new_session_path, notice: t("flash.confirmations.show.notice")
23
27
  else
24
- redirect_to new_confirmation_path, alert: "Confirmation link is invalid or has expired."
28
+ redirect_to new_confirmation_path, alert: t("flash.confirmations.show.alert.invalid_token")
25
29
  end
26
30
  end
27
31
  end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class InvitationsController < ApplicationController
2
- allow_unauthenticated_access only: %i[ edit update ]
4
+ require_unauthenticated_access only: %i[ edit update ]
3
5
  before_action :set_user_by_token, only: %i[ edit update ]
4
6
 
5
7
  def new
@@ -9,7 +11,7 @@ class InvitationsController < ApplicationController
9
11
  user = User.invite!(invitation_params, invited_by: Current.user)
10
12
 
11
13
  if user.persisted?
12
- redirect_to new_invitation_path, notice: "Invitation sent to #{user.email_address}."
14
+ redirect_to new_invitation_path, notice: t("flash.invitations.create.notice", email: user[authentication_email_column_name])
13
15
  else
14
16
  redirect_to new_invitation_path, alert: user.errors.full_messages.to_sentence
15
17
  end
@@ -21,7 +23,7 @@ class InvitationsController < ApplicationController
21
23
  def update
22
24
  if @user.accept_invitation!(password: params[:password], password_confirmation: params[:password_confirmation])
23
25
  start_new_session_for @user
24
- redirect_to after_authentication_url, notice: "Welcome! Your account is ready."
26
+ redirect_to after_authentication_url, notice: t("flash.invitations.update.notice")
25
27
  else
26
28
  redirect_to edit_invitation_path(params[:token]), alert: @user.errors.full_messages.to_sentence
27
29
  end
@@ -31,11 +33,18 @@ class InvitationsController < ApplicationController
31
33
 
32
34
  # Add any other attributes your User requires (e.g. :name) here and to the invitation form.
33
35
  def invitation_params
34
- params.permit(:email_address)
36
+ params.permit(authentication_email_column_name)
35
37
  end
36
38
 
37
39
  def set_user_by_token
38
40
  @user = User.find_by_valid_invitation_token(params[:token])
39
- redirect_to new_session_path, alert: "Invitation link is invalid or has expired." unless @user
41
+ return if @user
42
+
43
+ alert = if action_name == "edit"
44
+ t("flash.invitations.edit.alert.invalid_token")
45
+ else
46
+ t("flash.invitations.update.alert.invalid_token")
47
+ end
48
+ redirect_to new_session_path, alert: alert
40
49
  end
41
50
  end
@@ -1,42 +1,44 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class MagicLinksController < ApplicationController
2
- allow_unauthenticated_access
3
- rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_magic_link_path, alert: "Try again later." }
4
+ require_unauthenticated_access
5
+ rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_magic_link_path, alert: t("flash.magic_links.create.alert.rate_limited") }
4
6
 
5
7
  def new
6
8
  end
7
9
 
8
10
  def create
9
- user = User.find_by(email_address: params[:email_address])
11
+ user = User.find_by(authentication_email_column_name => params[authentication_email_column_name])
10
12
  user&.send_magic_link
11
13
 
12
- redirect_to new_session_path, notice: "Sign-in link sent (if a user with that email address exists)."
14
+ redirect_to new_session_path, notice: t("flash.magic_links.create.notice")
13
15
  end
14
16
 
15
17
  def show
16
18
  user = User.find_by_valid_magic_link_token(params[:token])
17
19
 
18
20
  if user.nil?
19
- redirect_to new_magic_link_path, alert: "Sign-in link is invalid or has expired."
21
+ redirect_to new_magic_link_path, alert: t("flash.magic_links.show.alert.invalid_token")
20
22
  <% if lockable? -%>
21
23
  elsif user.locked?
22
24
  <% if trackable? -%>
23
25
  record_authentication_attempt(user, success: false, failure_reason: "locked")
24
26
  <% end -%>
25
- redirect_to new_session_path, alert: "Your account is locked. Check your email for unlock instructions."
27
+ redirect_to new_session_path, alert: t("flash.magic_links.show.alert.locked")
26
28
  <% end -%>
27
29
  <% if confirmable? -%>
28
30
  elsif !user.confirmed?
29
31
  <% if trackable? -%>
30
32
  record_authentication_attempt(user, success: false, failure_reason: "unconfirmed")
31
33
  <% end -%>
32
- redirect_to new_session_path, alert: "You must confirm your email address before signing in."
34
+ redirect_to new_session_path, alert: t("flash.magic_links.show.alert.unconfirmed")
33
35
  <% end -%>
34
36
  <% if invitable? -%>
35
37
  elsif user.invitation_pending?
36
38
  <% if trackable? -%>
37
39
  record_authentication_attempt(user, success: false, failure_reason: "invitation_pending")
38
40
  <% end -%>
39
- redirect_to new_session_path, alert: "You must accept your invitation before signing in."
41
+ redirect_to new_session_path, alert: t("flash.magic_links.show.alert.invitation_pending")
40
42
  <% end -%>
41
43
  # Add your own authentication restrictions specific to your application
42
44
  # elsif user.deleted_at.present?
@@ -1,36 +1,39 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class OttsController < ApplicationController
2
- allow_unauthenticated_access
3
- rate_limit to: 10, within: 3.minutes, only: %i[ create update ], with: -> { redirect_to new_session_path, alert: "Try again later." }
4
+ require_unauthenticated_access
5
+ rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_session_path, alert: t("flash.otts.create.alert.rate_limited") }
6
+ rate_limit to: 10, within: 3.minutes, only: :update, with: -> { redirect_to new_session_path, alert: t("flash.otts.update.alert.rate_limited") }
4
7
 
5
8
  def create
6
- email_address = params[:email_address].presence || session[:ott_email_address]
7
- user = User.find_by(email_address: email_address)
9
+ email = params[authentication_email_column_name].presence || session[:ott_email]
10
+ user = User.find_by(authentication_email_column_name => email)
8
11
  user&.send_ott
9
- session[:ott_email_address] = email_address
12
+ session[:ott_email] = email
10
13
 
11
- redirect_to edit_ott_path, notice: "We've emailed you a sign-in code (if a user with that email address exists)."
14
+ redirect_to edit_ott_path, notice: t("flash.otts.create.notice")
12
15
  end
13
16
 
14
17
  def edit
15
- redirect_to new_session_path if session[:ott_email_address].blank?
18
+ redirect_to new_session_path if session[:ott_email].blank?
16
19
  end
17
20
 
18
21
  def update
19
- user = User.find_by(email_address: session[:ott_email_address])
22
+ user = User.find_by(authentication_email_column_name => session[:ott_email])
20
23
 
21
24
  case user && user.verify_ott(params[:code])
22
25
  when nil, :expired
23
- redirect_to new_session_path, alert: "Your sign-in code is invalid or has expired. Request a new one."
26
+ redirect_to new_session_path, alert: t("flash.otts.update.alert.invalid_code")
24
27
  when :exhausted
25
28
  <% if trackable? -%>
26
29
  record_authentication_attempt(user, success: false, failure_reason: "ott_attempts_exhausted")
27
30
  <% end -%>
28
- redirect_to new_session_path, alert: "Too many incorrect attempts. Request a new code."
31
+ redirect_to new_session_path, alert: t("flash.otts.update.alert.too_many_attempts")
29
32
  when :invalid
30
33
  <% if trackable? -%>
31
34
  record_authentication_attempt(user, success: false, failure_reason: "invalid_ott")
32
35
  <% end -%>
33
- redirect_to edit_ott_path, alert: "Incorrect code. Try again."
36
+ redirect_to edit_ott_path, alert: t("flash.otts.update.alert.incorrect_code")
34
37
  else
35
38
  complete_ott_sign_in(user)
36
39
  end
@@ -44,7 +47,7 @@ class OttsController < ApplicationController
44
47
  <% if trackable? -%>
45
48
  record_authentication_attempt(user, success: false, failure_reason: "locked")
46
49
  <% end -%>
47
- return redirect_to new_session_path, alert: "Your account is locked. Check your email for unlock instructions."
50
+ return redirect_to new_session_path, alert: t("flash.otts.update.alert.locked")
48
51
  end
49
52
  <% end -%>
50
53
  <% if confirmable? -%>
@@ -52,7 +55,7 @@ class OttsController < ApplicationController
52
55
  <% if trackable? -%>
53
56
  record_authentication_attempt(user, success: false, failure_reason: "unconfirmed")
54
57
  <% end -%>
55
- return redirect_to new_session_path, alert: "You must confirm your email address before signing in."
58
+ return redirect_to new_session_path, alert: t("flash.otts.update.alert.unconfirmed")
56
59
  end
57
60
  <% end -%>
58
61
  <% if invitable? -%>
@@ -60,7 +63,7 @@ class OttsController < ApplicationController
60
63
  <% if trackable? -%>
61
64
  record_authentication_attempt(user, success: false, failure_reason: "invitation_pending")
62
65
  <% end -%>
63
- return redirect_to new_session_path, alert: "You must accept your invitation before signing in."
66
+ return redirect_to new_session_path, alert: t("flash.otts.update.alert.invitation_pending")
64
67
  end
65
68
  <% end -%>
66
69
  # Add your own authentication restrictions specific to your application
@@ -72,7 +75,7 @@ class OttsController < ApplicationController
72
75
  # end
73
76
 
74
77
  user.consume_ott!
75
- session.delete(:ott_email_address)
78
+ session.delete(:ott_email)
76
79
  <% if lockable? -%>
77
80
  user.reset_failed_attempts!
78
81
  <% end -%>
@@ -1,13 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class PasskeySessionsController < ApplicationController
2
- allow_unauthenticated_access
3
- rate_limit to: 10, within: 3.minutes, only: :create, with: -> { render json: { error: "Try again later." }, status: :too_many_requests }
4
+ require_unauthenticated_access
5
+ rate_limit to: 10, within: 3.minutes, only: :create, with: -> { render json: { error: t("json.passkey_sessions.create.alert.rate_limited") }, status: :too_many_requests }
4
6
 
5
7
  def create
6
8
  webauthn_credential = WebAuthn::Credential.from_get(params)
7
9
  stored_credential = WebauthnCredential.find_by(external_id: webauthn_credential.id)
8
10
 
9
11
  if stored_credential.nil?
10
- return render json: { error: "Passkey not recognized." }, status: :unprocessable_content
12
+ return render json: { error: t("json.passkey_sessions.create.alert.not_recognized") }, status: :unprocessable_content
11
13
  end
12
14
 
13
15
  begin
@@ -17,7 +19,7 @@ class PasskeySessionsController < ApplicationController
17
19
  sign_count: stored_credential.sign_count
18
20
  )
19
21
  rescue WebAuthn::Error
20
- return render json: { error: "Passkey verification failed." }, status: :unprocessable_content
22
+ return render json: { error: t("json.passkey_sessions.create.alert.verification_failed") }, status: :unprocessable_content
21
23
  end
22
24
 
23
25
  stored_credential.update!(sign_count: webauthn_credential.sign_count)
@@ -29,7 +31,7 @@ class PasskeySessionsController < ApplicationController
29
31
  <% if trackable? -%>
30
32
  record_authentication_attempt(user, success: false, failure_reason: "locked")
31
33
  <% end -%>
32
- return render json: { error: "Your account is locked. Check your email for unlock instructions." }, status: :unprocessable_content
34
+ return render json: { error: t("json.passkey_sessions.create.alert.locked") }, status: :unprocessable_content
33
35
  end
34
36
  <% end -%>
35
37
  <% if confirmable? -%>
@@ -37,7 +39,7 @@ class PasskeySessionsController < ApplicationController
37
39
  <% if trackable? -%>
38
40
  record_authentication_attempt(user, success: false, failure_reason: "unconfirmed")
39
41
  <% end -%>
40
- return render json: { error: "You must confirm your email address before signing in." }, status: :unprocessable_content
42
+ return render json: { error: t("json.passkey_sessions.create.alert.unconfirmed") }, status: :unprocessable_content
41
43
  end
42
44
  <% end -%>
43
45
  <% if invitable? -%>
@@ -45,7 +47,7 @@ class PasskeySessionsController < ApplicationController
45
47
  <% if trackable? -%>
46
48
  record_authentication_attempt(user, success: false, failure_reason: "invitation_pending")
47
49
  <% end -%>
48
- return render json: { error: "You must accept your invitation before signing in." }, status: :unprocessable_content
50
+ return render json: { error: t("json.passkey_sessions.create.alert.invitation_pending") }, status: :unprocessable_content
49
51
  end
50
52
  <% end -%>
51
53
  <% if lockable? -%>
@@ -1,32 +1,38 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class PasswordsController < ApplicationController
2
- allow_unauthenticated_access
4
+ require_unauthenticated_access
3
5
  before_action :set_user_by_token, only: %i[ edit update ]
4
- rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_password_path, alert: "Try again later." }
6
+ rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_password_path, alert: t("flash.passwords.create.alert.rate_limited") }
5
7
 
6
8
  def new
7
9
  end
8
10
 
9
11
  def create
10
- if user = User.find_by(email_address: params[:email_address])
12
+ if user = User.find_by(authentication_email_column_name => params[authentication_email_column_name])
11
13
  user.send_reset_password_instructions
12
14
  end
13
15
 
14
- redirect_to new_session_path, notice: "Password reset instructions sent (if user with that email address exists)."
16
+ redirect_to new_session_path, notice: t("flash.passwords.create.notice")
15
17
  end
16
18
 
17
19
  def edit
18
20
  end
19
21
 
20
22
  def update
21
- if @user.update(params.permit(:password, :password_confirmation))
23
+ @user.assign_attributes(params.permit(:password, :password_confirmation))
24
+
25
+ if @user.save(context: :password_reset)
22
26
  @user.clear_reset_password_token!
23
27
  <% if lockable? -%>
24
28
  @user.unlock!
25
29
  <% end -%>
26
- @user.sessions.destroy_all
27
- redirect_to new_session_path, notice: "Password has been reset."
30
+ terminate_all_sessions_for(@user)
31
+ redirect_to new_session_path, notice: t("flash.passwords.update.notice")
32
+ elsif @user.errors.of_kind?(:password, :blank)
33
+ redirect_to edit_password_path(params[:token]), alert: t("flash.passwords.update.alert.password_blank")
28
34
  else
29
- redirect_to edit_password_path(params[:token]), alert: "Passwords did not match."
35
+ redirect_to edit_password_path(params[:token]), alert: t("flash.passwords.update.alert.password_mismatch")
30
36
  end
31
37
  end
32
38
 
@@ -34,6 +40,13 @@ class PasswordsController < ApplicationController
34
40
 
35
41
  def set_user_by_token
36
42
  @user = User.find_by_valid_reset_password_token(params[:token])
37
- redirect_to new_password_path, alert: "Password reset link is invalid or has expired." unless @user
43
+ return if @user
44
+
45
+ alert = if action_name == "edit"
46
+ t("flash.passwords.edit.alert.invalid_token")
47
+ else
48
+ t("flash.passwords.update.alert.invalid_token")
49
+ end
50
+ redirect_to new_password_path, alert: alert
38
51
  end
39
52
  end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class RegistrationsController < ApplicationController
2
- allow_unauthenticated_access only: %i[ new create ]
4
+ require_unauthenticated_access only: %i[ new create ]
3
5
 
4
6
  def new
5
7
  @user = User.new
@@ -11,10 +13,10 @@ class RegistrationsController < ApplicationController
11
13
  if @user.save
12
14
  <% if confirmable? -%>
13
15
  @user.send_confirmation_instructions
14
- redirect_to new_session_path, notice: "Welcome! Check your email to confirm your account."
16
+ redirect_to new_session_path, notice: t("flash.registrations.create.notice")
15
17
  <% else -%>
16
18
  start_new_session_for @user
17
- redirect_to after_authentication_url, notice: "Welcome! You have signed up successfully."
19
+ redirect_to after_authentication_url, notice: t("flash.registrations.create.notice")
18
20
  <% end -%>
19
21
  else
20
22
  render :new, status: :unprocessable_content
@@ -29,7 +31,7 @@ class RegistrationsController < ApplicationController
29
31
  @user = Current.user
30
32
 
31
33
  if @user.update(user_params_for_update)
32
- redirect_to edit_registration_path, notice: "Your account has been updated."
34
+ redirect_to edit_registration_path, notice: t("flash.registrations.update.notice")
33
35
  else
34
36
  render :edit, status: :unprocessable_content
35
37
  end
@@ -39,13 +41,13 @@ class RegistrationsController < ApplicationController
39
41
  user = Current.user
40
42
  terminate_session
41
43
  user.destroy
42
- redirect_to new_session_path, notice: "Your account has been deleted.", status: :see_other
44
+ redirect_to new_session_path, notice: t("flash.registrations.destroy.notice"), status: :see_other
43
45
  end
44
46
 
45
47
  private
46
48
 
47
49
  def user_params
48
- params.permit(:email_address, :password, :password_confirmation)
50
+ params.permit(authentication_email_column_name, :password, :password_confirmation)
49
51
  end
50
52
 
51
53
  def user_params_for_update
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class SessionsController < ApplicationController
2
- allow_unauthenticated_access only: %i[ new create ]
3
- rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_session_path, alert: "Try again later." }
4
+ require_unauthenticated_access only: %i[ new create ]
5
+ rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_session_path, alert: t("flash.sessions.create.alert.rate_limited") }
4
6
 
5
7
  def new
6
8
  <% if passkey? -%>
@@ -10,11 +12,11 @@ class SessionsController < ApplicationController
10
12
  end
11
13
 
12
14
  def create
13
- user = User.authenticate_by(params.permit(:email_address, :password))
15
+ user = User.authenticate_by(params.permit(authentication_email_column_name, :password))
14
16
 
15
17
  if user.nil?
16
18
  <% if lockable? || trackable? -%>
17
- account = User.find_by(email_address: params[:email_address])
19
+ account = User.find_by(authentication_email_column_name => params[authentication_email_column_name])
18
20
  <% end -%>
19
21
  <% if lockable? -%>
20
22
  account&.register_failed_attempt!
@@ -22,27 +24,27 @@ class SessionsController < ApplicationController
22
24
  <% if trackable? -%>
23
25
  record_authentication_attempt(account, success: false, failure_reason: "invalid_credentials")
24
26
  <% end -%>
25
- redirect_to new_session_path, alert: "Try another email address or password."
27
+ redirect_to new_session_path, alert: t("flash.sessions.create.alert.invalid")
26
28
  <% if lockable? -%>
27
29
  elsif user.locked?
28
30
  <% if trackable? -%>
29
31
  record_authentication_attempt(user, success: false, failure_reason: "locked")
30
32
  <% end -%>
31
- redirect_to new_session_path, alert: "Your account is locked. Check your email for unlock instructions."
33
+ redirect_to new_session_path, alert: t("flash.sessions.create.alert.locked")
32
34
  <% end -%>
33
35
  <% if confirmable? -%>
34
36
  elsif !user.confirmed?
35
37
  <% if trackable? -%>
36
38
  record_authentication_attempt(user, success: false, failure_reason: "unconfirmed")
37
39
  <% end -%>
38
- redirect_to new_session_path, alert: "You must confirm your email address before signing in."
40
+ redirect_to new_session_path, alert: t("flash.sessions.create.alert.unconfirmed")
39
41
  <% end -%>
40
42
  <% if invitable? -%>
41
43
  elsif user.invitation_pending?
42
44
  <% if trackable? -%>
43
45
  record_authentication_attempt(user, success: false, failure_reason: "invitation_pending")
44
46
  <% end -%>
45
- redirect_to new_session_path, alert: "You must accept your invitation before signing in."
47
+ redirect_to new_session_path, alert: t("flash.sessions.create.alert.invitation_pending")
46
48
  <% end -%>
47
49
  # Add your own authentication restrictions specific to your application
48
50
  # elsif user.deleted_at.present?
@@ -68,7 +70,7 @@ class SessionsController < ApplicationController
68
70
 
69
71
  def destroy
70
72
  terminate_session
71
- redirect_to new_session_path, status: :see_other
73
+ redirect_to new_session_path, status: :see_other, notice: t("flash.sessions.destroy.notice")
72
74
  end
73
75
  <% if trackable? -%>
74
76
 
@@ -1,23 +1,25 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class UnlocksController < ApplicationController
2
- allow_unauthenticated_access
3
- rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_unlock_path, alert: "Try again later." }
4
+ require_unauthenticated_access
5
+ rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_unlock_path, alert: t("flash.unlocks.create.alert.rate_limited") }
4
6
 
5
7
  def new
6
8
  end
7
9
 
8
10
  def create
9
- user = User.find_by(email_address: params[:email_address])
11
+ user = User.find_by(authentication_email_column_name => params[authentication_email_column_name])
10
12
  user.send_unlock_instructions if user&.locked?
11
13
 
12
- redirect_to new_session_path, notice: "Unlock instructions sent (if user with that email address exists and is locked)."
14
+ redirect_to new_session_path, notice: t("flash.unlocks.create.notice")
13
15
  end
14
16
 
15
17
  def show
16
18
  if user = User.find_by_valid_unlock_token(params[:token])
17
19
  user.unlock!
18
- redirect_to new_session_path, notice: "Your account has been unlocked. Please sign in."
20
+ redirect_to new_session_path, notice: t("flash.unlocks.show.notice")
19
21
  else
20
- redirect_to new_unlock_path, alert: "Unlock link is invalid or has expired."
22
+ redirect_to new_unlock_path, alert: t("flash.unlocks.show.alert.invalid_token")
21
23
  end
22
24
  end
23
25
  end