Skip to content

[Security] Update locale with new authentication system #17595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions session.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1469,16 +1469,15 @@ this as the locale for the given user. To accomplish this, you can hook into
the login process and update the user's session with this locale value before
they are redirected to their first page.

To do this, you need an event subscriber on the ``security.interactive_login``
To do this, you need an event subscriber on the ``LoginSuccessEvent::class``
event::

// src/EventSubscriber/UserLocaleSubscriber.php
namespace App\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;
use Symfony\Component\Security\Http\Event\LoginSuccessEvent;

/**
* Stores the locale of the user in the session after the
Expand All @@ -1491,9 +1490,9 @@ event::
) {
}

public function onInteractiveLogin(InteractiveLoginEvent $event): void
public function onLoginSuccess(LoginSuccessEvent $event): void
{
$user = $event->getAuthenticationToken()->getUser();
$user = $event->getUser();

if (null !== $user->getLocale()) {
$this->requestStack->getSession()->set('_locale', $user->getLocale());
Expand All @@ -1503,7 +1502,7 @@ event::
public static function getSubscribedEvents(): array
{
return [
SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
LoginSuccessEvent::class => 'onLoginSuccess',
];
}
}
Expand Down