From db2e92c0583c327d042b5216c7bf19e7320f64c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Sat, 10 Jan 2026 11:32:14 +0100 Subject: [PATCH] feat: custom hx redirect class --- src/Htmx/HxRedirectResponse.php | 22 ++++++++++++++++++++++ src/Htmx/HxTrait.php | 5 +---- src/Security/BpnAuthenticator.php | 12 +++++++++--- 3 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 src/Htmx/HxRedirectResponse.php diff --git a/src/Htmx/HxRedirectResponse.php b/src/Htmx/HxRedirectResponse.php new file mode 100644 index 0000000..cf0c61c --- /dev/null +++ b/src/Htmx/HxRedirectResponse.php @@ -0,0 +1,22 @@ + $url]); + } +} diff --git a/src/Htmx/HxTrait.php b/src/Htmx/HxTrait.php index 616a372..3069718 100644 --- a/src/Htmx/HxTrait.php +++ b/src/Htmx/HxTrait.php @@ -58,10 +58,7 @@ trait HxTrait protected function htmxRedirect(Request $request, string $url): Response { if ($request->headers->has('HX-Request')) { - $response = new Response('', Response::HTTP_OK); - $response->headers->set('HX-Redirect', $url); - - return $response; + return new HxRedirectResponse($url); } return $this->redirect($url); diff --git a/src/Security/BpnAuthenticator.php b/src/Security/BpnAuthenticator.php index 962d40b..e7cba36 100644 --- a/src/Security/BpnAuthenticator.php +++ b/src/Security/BpnAuthenticator.php @@ -8,6 +8,7 @@ use App\BusProNet\ApiClient; use App\BusProNet\Exception\ApiClientException; use App\BusProNet\Model\PersonalData; use App\Entity\User; +use App\Htmx\HxRedirectResponse; use Doctrine\ORM\EntityManagerInterface; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -119,10 +120,15 @@ class BpnAuthenticator extends AbstractLoginFormAuthenticator implements Authent 'email' => $token->getUserIdentifier(), ]); - if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) { - return new RedirectResponse($targetPath); + $targetPath = $this->getTargetPath($request->getSession(), $firewallName) + ?? $this->urlGenerator->generate('app_account'); + + // When redirecting to admin from an HTMX request, force a full page navigation + // to avoid layout issues between frontend (hx-boost) and EasyAdmin + if ($request->headers->has('HX-Request') && str_contains($targetPath, '/admin')) { + return new HxRedirectResponse($targetPath); } - return new RedirectResponse($this->urlGenerator->generate('app_account')); + return new RedirectResponse($targetPath); } }