Keep the chosen language across sign-in and forced sign-out
A language picked on a signed-out page is now stored on the account when the user signs in (StoreLocaleChosenBeforeLogin), so the app does not switch back to the account default. EnsureAccountIsUsable keeps the user's language for the login page when it ends a disabled account's session. ChangePreferredLocale runs in a transaction like the other use cases, the language names come from layout.languages instead of the Locale enum, and the translation test also compares fi.json with en.json and rejects English files without a Finnish counterpart. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -30,6 +30,8 @@ class SwitchLocaleController
|
||||
|
||||
// The signed-in user object may outlive this request (e.g. Octane, in-process browser tests).
|
||||
$user->locale = $locale;
|
||||
} else {
|
||||
$request->session()->put(SetLocale::CHOSEN_SESSION_KEY, true);
|
||||
}
|
||||
|
||||
$request->session()->put(SetLocale::SESSION_KEY, $locale->value);
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Users\Presentation\Http\Listeners;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Modules\Users\Application\UseCases\ChangePreferredLocale;
|
||||
use App\Modules\Users\Domain\Enums\Locale;
|
||||
use App\Modules\Users\Presentation\Http\Middleware\SetLocale;
|
||||
use Illuminate\Auth\Events\Login;
|
||||
use Illuminate\Contracts\Session\Session;
|
||||
|
||||
/**
|
||||
* A language the user picked on a signed-out page (login, forgot or set
|
||||
* password) becomes their account's language when they sign in, so the
|
||||
* pages after signing in stay in the language they just chose.
|
||||
*/
|
||||
class StoreLocaleChosenBeforeLogin
|
||||
{
|
||||
public function __construct(
|
||||
private Session $session,
|
||||
private ChangePreferredLocale $changeLocale,
|
||||
) {}
|
||||
|
||||
public function handle(Login $event): void
|
||||
{
|
||||
if (! $event->user instanceof User || ! $this->session->pull(SetLocale::CHOSEN_SESSION_KEY, false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$locale = Locale::tryFrom((string) $this->session->get(SetLocale::SESSION_KEY));
|
||||
|
||||
if ($locale === null || $locale === $event->user->locale) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->changeLocale->execute($event->user->id, $locale);
|
||||
$event->user->locale = $locale;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Modules\Users\Presentation\Http\Middleware;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Modules\Users\Application\UseCases\CheckLoginAllowed;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -21,10 +22,18 @@ class EnsureAccountIsUsable
|
||||
$userId = Auth::id();
|
||||
|
||||
if ($userId !== null && ! $this->checkLoginAllowed->execute((int) $userId)) {
|
||||
$user = $request->user();
|
||||
$locale = $user instanceof User ? $user->locale->value : null;
|
||||
|
||||
Auth::guard('web')->logout();
|
||||
$request->session()->invalidate();
|
||||
$request->session()->regenerateToken();
|
||||
|
||||
// The login page explains why, in the language the user had chosen.
|
||||
if ($locale !== null) {
|
||||
$request->session()->put(SetLocale::SESSION_KEY, $locale);
|
||||
}
|
||||
|
||||
// The reason goes in the URL: Livewire follows this redirect with
|
||||
// fetch first, which would consume a flashed message.
|
||||
return redirect()->route('login', ['access' => 'ended']);
|
||||
|
||||
@@ -19,6 +19,12 @@ class SetLocale
|
||||
{
|
||||
public const SESSION_KEY = 'locale';
|
||||
|
||||
/**
|
||||
* Set when the user picked the language on a signed-out page: signing in
|
||||
* then stores it on the account (StoreLocaleChosenBeforeLogin).
|
||||
*/
|
||||
public const CHOSEN_SESSION_KEY = 'locale_chosen';
|
||||
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
$user = $request->user();
|
||||
|
||||
Reference in New Issue
Block a user