diff --git a/src/Controllers/AuthenticationController.php b/src/Controllers/AuthenticationController.php index 7972ceb..5001c3d 100644 --- a/src/Controllers/AuthenticationController.php +++ b/src/Controllers/AuthenticationController.php @@ -38,7 +38,7 @@ public function login(ServerRequestInterface $request, ResponseInterface $respon $this->authenticationService->authenticate($data, true); return $response - ->withHeader('Location', '/') + ->withHeader('Location', $this->redirectTo($request)) ->withStatus(302); } catch (AuthenticationException) { $error = 'Login failed'; @@ -167,4 +167,21 @@ private function render(ResponseInterface $response, string $template, array $co return $response; } + + private function redirectTo(ServerRequestInterface $request): string + { + $redirect = $request->getQueryParams()['redirect'] ?? '/'; + + if (! $redirect || str_starts_with($redirect, '//') || str_ends_with(rtrim($redirect, '/'), '/login')) { + return '/'; + } + + $rootUrl = $request->getUri()->getScheme() . '://' . $request->getUri()->getHost(); + + if (str_starts_with($redirect, $request->getUri()->getScheme())) { + return str_starts_with($redirect, $rootUrl) ? $redirect : '/'; + } + + return '/' . trim((string)$redirect, '/'); + } }