Skip to content

Commit 1bf9fd1

Browse files
committed
Use Security::logout instead of redirecting to the logout url
1 parent c8bafc3 commit 1bf9fd1

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

config/services.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,3 @@ services:
2929
- '../src/DependencyInjection/'
3030
- '../src/Entity/'
3131
- '../src/Kernel.php'
32-
33-
# this is needed because Symfony doesn't make the 'security.logout_url_generator' service autowirable
34-
Symfony\Component\Security\Http\Logout\LogoutUrlGenerator: '@security.logout_url_generator'

src/Controller/UserController.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
use App\Form\UserType;
1717
use Doctrine\ORM\EntityManagerInterface;
1818
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
19+
use Symfony\Bundle\SecurityBundle\Security;
1920
use Symfony\Component\HttpFoundation\Request;
2021
use Symfony\Component\HttpFoundation\Response;
2122
use Symfony\Component\Routing\Annotation\Route;
2223
use Symfony\Component\Security\Http\Attribute\CurrentUser;
2324
use Symfony\Component\Security\Http\Attribute\IsGranted;
24-
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
2525

2626
/**
2727
* Controller used to manage current user. The #[CurrentUser] attribute
@@ -62,15 +62,19 @@ public function changePassword(
6262
#[CurrentUser] User $user,
6363
Request $request,
6464
EntityManagerInterface $entityManager,
65-
LogoutUrlGenerator $logoutUrlGenerator,
65+
Security $security,
6666
): Response {
6767
$form = $this->createForm(ChangePasswordType::class, $user);
6868
$form->handleRequest($request);
6969

7070
if ($form->isSubmitted() && $form->isValid()) {
7171
$entityManager->flush();
7272

73-
return $this->redirect($logoutUrlGenerator->getLogoutPath());
73+
// The second argument is to disable CSRF protection before logout.
74+
// A CSRF token is already verified by the form.
75+
$security->logout(false);
76+
77+
return $this->redirectToRoute('security_login');
7478
}
7579

7680
return $this->render('user/change_password.html.twig', [

0 commit comments

Comments
 (0)