From e8f17dc10aa77016823dabc0f038105a4385e41b Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Thu, 20 Nov 2025 16:54:41 +0100 Subject: [PATCH] Allow to pass extra data when handling success authentication --- .../AuthenticationSuccessHandler.php | 6 +++--- .../AuthenticationSuccessHandlerTest.php | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Security/Http/Authentication/AuthenticationSuccessHandler.php b/Security/Http/Authentication/AuthenticationSuccessHandler.php index 04be1064..2ded3812 100644 --- a/Security/Http/Authentication/AuthenticationSuccessHandler.php +++ b/Security/Http/Authentication/AuthenticationSuccessHandler.php @@ -48,7 +48,7 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token) return $this->handleAuthenticationSuccess($token->getUser()); } - public function handleAuthenticationSuccess(UserInterface $user, $jwt = null): Response + public function handleAuthenticationSuccess(UserInterface $user, $jwt = null, array $data = []): Response { if (null === $jwt) { $jwt = $this->jwtManager->create($user); @@ -59,8 +59,8 @@ public function handleAuthenticationSuccess(UserInterface $user, $jwt = null): R $jwtCookies[] = $cookieProvider->createCookie($jwt); } - $response = new JWTAuthenticationSuccessResponse($jwt, [], $jwtCookies); - $event = new AuthenticationSuccessEvent(['token' => $jwt], $user, $response); + $response = new JWTAuthenticationSuccessResponse($jwt, $data, $jwtCookies); + $event = new AuthenticationSuccessEvent(['token' => $jwt] + $data, $user, $response); $this->dispatcher->dispatch($event, Events::AUTHENTICATION_SUCCESS); $responseData = $event->getData(); diff --git a/Tests/Security/Http/Authentication/AuthenticationSuccessHandlerTest.php b/Tests/Security/Http/Authentication/AuthenticationSuccessHandlerTest.php index 85da30bd..aeebce5f 100644 --- a/Tests/Security/Http/Authentication/AuthenticationSuccessHandlerTest.php +++ b/Tests/Security/Http/Authentication/AuthenticationSuccessHandlerTest.php @@ -69,6 +69,21 @@ public function testHandleAuthenticationSuccessWithGivenJWT() $this->assertSame('jwt', $content['token']); } + public function testHandleAuthenticationSuccessWithExtraData() + { + $response = (new AuthenticationSuccessHandler($this->getJWTManager('secrettoken'), $this->getDispatcher())) + ->handleAuthenticationSuccess($this->getUser(), null, ['state' => 'foo']); + + $this->assertInstanceOf(JsonResponse::class, $response); + $this->assertSame(Response::HTTP_OK, $response->getStatusCode(), $response->getContent()); + + $content = json_decode($response->getContent(), true); + $this->assertArrayHasKey('token', $content); + $this->assertSame('secrettoken', $content['token']); + $this->assertArrayHasKey('state', $content); + $this->assertSame('foo', $content['state']); + } + public function testOnAuthenticationSuccessSetCookie() { $request = $this->getRequest();