Skip to content

Commit 051f94a

Browse files
Pass locale with activatable / timeoutable hooks (#5815)
We need to explicitly pass the `locale` around from the options (passed to `warden.authenticate!` for instance) or the `I18n.locale` when logging out and redirecting the user via `throw :warden`, otherwise in a multi-locale app we'd lose the locale previously set / passed around and fallback to the default for that flash message. This is a follow-up of the fixes in #5567 where we implemented the locale passing logic down to the failure app, but it missed these places where we were using `throw :warden`. Closes #5812
1 parent cd9c21a commit 051f94a

File tree

7 files changed

+35
-9
lines changed

7 files changed

+35
-9
lines changed

lib/devise/hooks/activatable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
if record && record.respond_to?(:active_for_authentication?) && !record.active_for_authentication?
88
scope = options[:scope]
99
warden.logout(scope)
10-
throw :warden, scope: scope, message: record.inactive_message
10+
throw :warden, scope: scope, message: record.inactive_message, locale: options.fetch(:locale, I18n.locale)
1111
end
1212
end

lib/devise/hooks/timeoutable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
record.timedout?(last_request_at) &&
2626
!proxy.remember_me_is_active?(record)
2727
Devise.sign_out_all_scopes ? proxy.sign_out : proxy.sign_out(scope)
28-
throw :warden, scope: scope, message: :timeout
28+
throw :warden, scope: scope, message: :timeout, locale: options.fetch(:locale, I18n.locale)
2929
end
3030

3131
unless env['devise.skip_trackable']

test/integration/confirmable_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ def resend_confirmation
136136
end
137137
end
138138

139+
test 'not confirmed user redirect respects i18n locale set' do
140+
swap Devise, allow_unconfirmed_access_for: 0.days do
141+
sign_in_as_user(confirm: false, visit: new_user_session_path(locale: "pt-BR"))
142+
143+
assert_contain 'Você precisa confirmar seu email para continuar'
144+
assert_not warden.authenticated?(:user)
145+
end
146+
end
147+
139148
test 'not confirmed user should not see confirmation message if invalid credentials are given' do
140149
swap Devise, allow_unconfirmed_access_for: 0.days do
141150
sign_in_as_user(confirm: false) do

test/integration/timeoutable_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,17 @@ def last_request_at
167167
end
168168
end
169169

170+
test 'error message redirect respects i18n locale set' do
171+
user = sign_in_as_user
172+
173+
get expire_user_path(user)
174+
get root_path(locale: "pt-BR")
175+
follow_redirect!
176+
177+
assert_contain 'Sua sessão expirou. Por favor faça o login novamente para continuar.'
178+
assert_not warden.authenticated?(:user)
179+
end
180+
170181
test 'time out not triggered if remembered' do
171182
user = sign_in_as_user remember_me: true
172183
get expire_user_path(user)
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
# frozen_string_literal: true
22

33
class AdminsController < ApplicationController
4-
around_action :set_locale
54
before_action :authenticate_admin!
65

76
def index
87
end
9-
10-
private
11-
12-
def set_locale
13-
I18n.with_locale(params[:locale] || I18n.default_locale) { yield }
14-
end
158
end

test/rails_app/app/controllers/application_controller.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,20 @@
55

66
class ApplicationController < ActionController::Base
77
protect_from_forgery
8+
around_action :set_locale
89
before_action :current_user, unless: :devise_controller?
910
before_action :authenticate_user!, if: :devise_controller?
1011
respond_to(*Mime::SET.map(&:to_sym))
1112

1213
devise_group :commenter, contains: [:user, :admin]
14+
15+
private
16+
17+
def set_locale
18+
I18n.with_locale(params[:locale] || I18n.default_locale) { yield }
19+
end
20+
21+
def default_url_options
22+
{locale: params[:locale]}.compact
23+
end
1324
end

test/support/locale/pt-BR.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ pt-BR:
33
failure:
44
invalid: "%{authentication_keys} ou senha inválidos."
55
unauthenticated: "Para continuar, faça login ou registre-se."
6+
timeout: "Sua sessão expirou. Por favor faça o login novamente para continuar."
7+
unconfirmed: "Você precisa confirmar seu email para continuar."

0 commit comments

Comments
 (0)