diff --git a/public/api/index.php b/public/api/index.php index 8cc7287..580f794 100644 --- a/public/api/index.php +++ b/public/api/index.php @@ -16,6 +16,7 @@ use App\Http\JsonHttpResponse; use App\Http\ViewHttpResponse; use App\Model\AuthModel; use App\Model\TacticModel; +use App\Session\PhpSessionHandle; use App\Session\SessionHandle; use App\Validation\ValidationFail; @@ -113,7 +114,7 @@ function tryGetAuthAccount(): ?Account { // If no authorization header is set, try fallback to php session. if (!isset($headers['Authorization'])) { - $session = SessionHandle::init(); + $session = PhpSessionHandle::init(); return $session->getAccount(); } diff --git a/public/index.php b/public/index.php index 7d42305..ce1df33 100644 --- a/public/index.php +++ b/public/index.php @@ -8,9 +8,9 @@ require "utils.php"; require "../src/react-display.php"; use App\Controller\FrontController; -use App\Session\SessionHandle; +use App\Session\PhpSessionHandle; $basePath = get_public_path(); $frontController = new FrontController($basePath); -$frontController->run(SessionHandle::init()); +$frontController->run(PhpSessionHandle::init()); diff --git a/src/Controller/FrontController.php b/src/Controller/FrontController.php index d1bbd6d..4ac3047 100644 --- a/src/Controller/FrontController.php +++ b/src/Controller/FrontController.php @@ -40,11 +40,12 @@ class FrontController { $match = $this->router->match(); if ($match) { $this->handleMatch($match, $session); - } else { - $this->displayViewByKind(ViewHttpResponse::twig("error.html.twig", [ - 'failures' => [ValidationFail::notFound("Could not find page ${_SERVER['REQUEST_URI']}.")], - ], HttpCodes::NOT_FOUND)); + return; } + + $this->displayViewByKind(ViewHttpResponse::twig("error.html.twig", [ + 'failures' => [ValidationFail::notFound("Could not find page ${_SERVER['REQUEST_URI']}.")], + ], HttpCodes::NOT_FOUND)); } /** @@ -96,6 +97,8 @@ class FrontController { if ($controllerName != self::VISITOR_CONTROLLER) { $account = $session->getAccount(); if ($account == null) { + // put in the session the initial url the user wanted to get + $session->setInitialTarget($_SERVER['REQUEST_URI']); return HttpResponse::redirect($this->basePath . "/visitor/login"); } } diff --git a/src/Controller/Sub/AuthController.php b/src/Controller/Sub/AuthController.php index fcf12d5..d94da43 100644 --- a/src/Controller/Sub/AuthController.php +++ b/src/Controller/Sub/AuthController.php @@ -63,7 +63,8 @@ class AuthController { $session->setAccount($account); - return ViewHttpResponse::twig("display_auth_confirm.html.twig", ['username' => $account->getName(), 'email' => $account->getEmail()]); + $target_url = $session->getInitialTarget(); + return HttpResponse::redirect($target_url ?? "/home"); } @@ -92,7 +93,9 @@ class AuthController { $session->setAccount($account); - return ViewHttpResponse::twig("display_auth_confirm.html.twig", ['username' => $account->getName(), 'email' => $account->getEmail()]); + $target_url = $session->getInitialTarget(); + $session->setInitialTarget(null); + return HttpResponse::redirect($target_url ?? "/home"); } } diff --git a/src/Session/MutableSessionHandle.php b/src/Session/MutableSessionHandle.php index 2bcb0fc..64ee52f 100644 --- a/src/Session/MutableSessionHandle.php +++ b/src/Session/MutableSessionHandle.php @@ -5,5 +5,8 @@ namespace App\Session; use App\Data\Account; interface MutableSessionHandle extends SessionHandle { + + public function setInitialTarget(?string $url): void; + public function setAccount(Account $account): void; } diff --git a/src/Session/PhpSessionHandle.php b/src/Session/PhpSessionHandle.php index 09d862c..5568eb5 100644 --- a/src/Session/PhpSessionHandle.php +++ b/src/Session/PhpSessionHandle.php @@ -17,7 +17,15 @@ class PhpSessionHandle implements MutableSessionHandle { return $_SESSION["account"] ?? null; } + public function getInitialTarget(): ?string { + return $_SESSION["target"] ?? null; + } + public function setAccount(Account $account): void { $_SESSION["account"] = $account; } + + public function setInitialTarget(?string $url): void { + $_SESSION["target"] = $url; + } } diff --git a/src/Session/SessionHandle.php b/src/Session/SessionHandle.php index 6ffb9c0..2114877 100644 --- a/src/Session/SessionHandle.php +++ b/src/Session/SessionHandle.php @@ -5,5 +5,7 @@ namespace App\Session; use App\Data\Account; interface SessionHandle { + public function getInitialTarget(): ?string; + public function getAccount(): ?Account; }