From 0fef612a780c29cec550fb97c458ff465f73a997 Mon Sep 17 00:00:00 2001 From: JVT038 <47184046+JVT038@users.noreply.github.com> Date: Sat, 30 Sep 2023 18:51:49 +0200 Subject: [PATCH 1/4] Automatically redirect user if not authenticated and redirect the user back if they have logged in --- src/HttpController/Web/AuthenticationController.php | 13 +++++++------ .../Web/Middleware/UserIsAuthenticated.php | 2 +- src/ValueObject/Http/Response.php | 6 ++++++ templates/page/login.html.twig | 6 ++++++ 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/HttpController/Web/AuthenticationController.php b/src/HttpController/Web/AuthenticationController.php index edaf6084..68bc2837 100644 --- a/src/HttpController/Web/AuthenticationController.php +++ b/src/HttpController/Web/AuthenticationController.php @@ -37,11 +37,12 @@ public function login(Request $request) : Response } catch (InvalidCredentials) { $this->sessionWrapper->set('failedLogin', true); } - + $redirect = $postParameters['redirect']; + $target = $redirect ?? $_SERVER['HTTP_REFERER']; return Response::create( StatusCode::createSeeOther(), null, - [Header::createLocation($_SERVER['HTTP_REFERER'])], + [Header::createLocation($target)], ); } @@ -56,20 +57,20 @@ public function logout() : Response ); } - public function renderLoginPage() : Response + public function renderLoginPage(Request $request) : Response { $failedLogin = $this->sessionWrapper->has('failedLogin'); + $redirect = $request->getGetParameters()['redirect'] ?? false; $this->sessionWrapper->unset('failedLogin'); $renderedTemplate = $this->twig->render( 'page/login.html.twig', [ - 'failedLogin' => $failedLogin + 'failedLogin' => $failedLogin, + 'redirect' => $redirect ], ); - $this->sessionWrapper->unset('failedLogin'); - return Response::create( StatusCode::createOk(), $renderedTemplate, diff --git a/src/HttpController/Web/Middleware/UserIsAuthenticated.php b/src/HttpController/Web/Middleware/UserIsAuthenticated.php index ddc0af62..104a6bcf 100644 --- a/src/HttpController/Web/Middleware/UserIsAuthenticated.php +++ b/src/HttpController/Web/Middleware/UserIsAuthenticated.php @@ -18,6 +18,6 @@ public function __invoke() : ?Response return null; } - return Response::createForbidden(); + return Response::createForbiddenRedirect($_SERVER['REQUEST_URI']); } } diff --git a/src/ValueObject/Http/Response.php b/src/ValueObject/Http/Response.php index 26dbb7e1..89dbb0ef 100644 --- a/src/ValueObject/Http/Response.php +++ b/src/ValueObject/Http/Response.php @@ -34,6 +34,12 @@ public static function createForbidden() : self return new self(StatusCode::createForbidden()); } + public static function createForbiddenRedirect(string $redirectTarget) : self + { + $query = urlencode($redirectTarget); + return new self(StatusCode::createForbidden(), null, [Header::createLocation('/login?redirect='.$query)]); + } + public static function createJson(string $body) : self { return new self(StatusCode::createOk(), $body, [Header::createContentTypeJson()]); diff --git a/templates/page/login.html.twig b/templates/page/login.html.twig index ee5c3896..bf3fa7b9 100644 --- a/templates/page/login.html.twig +++ b/templates/page/login.html.twig @@ -43,6 +43,12 @@ Invalid credentials {% endif %} + {% if redirect != false %} +