add target url in session to bring back user to its initial target after auth

pull/19/head
maxime.batista 1 year ago
parent 39329c9325
commit 69be939954

@ -16,6 +16,7 @@ use App\Http\JsonHttpResponse;
use App\Http\ViewHttpResponse; use App\Http\ViewHttpResponse;
use App\Model\AuthModel; use App\Model\AuthModel;
use App\Model\TacticModel; use App\Model\TacticModel;
use App\Session\PhpSessionHandle;
use App\Session\SessionHandle; use App\Session\SessionHandle;
use App\Validation\ValidationFail; use App\Validation\ValidationFail;
@ -113,7 +114,7 @@ function tryGetAuthAccount(): ?Account {
// If no authorization header is set, try fallback to php session. // If no authorization header is set, try fallback to php session.
if (!isset($headers['Authorization'])) { if (!isset($headers['Authorization'])) {
$session = SessionHandle::init(); $session = PhpSessionHandle::init();
return $session->getAccount(); return $session->getAccount();
} }

@ -8,9 +8,9 @@ require "utils.php";
require "../src/react-display.php"; require "../src/react-display.php";
use App\Controller\FrontController; use App\Controller\FrontController;
use App\Session\SessionHandle; use App\Session\PhpSessionHandle;
$basePath = get_public_path(); $basePath = get_public_path();
$frontController = new FrontController($basePath); $frontController = new FrontController($basePath);
$frontController->run(SessionHandle::init()); $frontController->run(PhpSessionHandle::init());

@ -40,12 +40,13 @@ class FrontController {
$match = $this->router->match(); $match = $this->router->match();
if ($match) { if ($match) {
$this->handleMatch($match, $session); $this->handleMatch($match, $session);
} else { return;
}
$this->displayViewByKind(ViewHttpResponse::twig("error.html.twig", [ $this->displayViewByKind(ViewHttpResponse::twig("error.html.twig", [
'failures' => [ValidationFail::notFound("Could not find page ${_SERVER['REQUEST_URI']}.")], 'failures' => [ValidationFail::notFound("Could not find page ${_SERVER['REQUEST_URI']}.")],
], HttpCodes::NOT_FOUND)); ], HttpCodes::NOT_FOUND));
} }
}
/** /**
* Create a new instance of an AltoRouter * Create a new instance of an AltoRouter
@ -96,6 +97,8 @@ class FrontController {
if ($controllerName != self::VISITOR_CONTROLLER) { if ($controllerName != self::VISITOR_CONTROLLER) {
$account = $session->getAccount(); $account = $session->getAccount();
if ($account == null) { 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"); return HttpResponse::redirect($this->basePath . "/visitor/login");
} }
} }

@ -63,7 +63,8 @@ class AuthController {
$session->setAccount($account); $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); $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");
} }
} }

@ -5,5 +5,8 @@ namespace App\Session;
use App\Data\Account; use App\Data\Account;
interface MutableSessionHandle extends SessionHandle { interface MutableSessionHandle extends SessionHandle {
public function setInitialTarget(?string $url): void;
public function setAccount(Account $account): void; public function setAccount(Account $account): void;
} }

@ -17,7 +17,15 @@ class PhpSessionHandle implements MutableSessionHandle {
return $_SESSION["account"] ?? null; return $_SESSION["account"] ?? null;
} }
public function getInitialTarget(): ?string {
return $_SESSION["target"] ?? null;
}
public function setAccount(Account $account): void { public function setAccount(Account $account): void {
$_SESSION["account"] = $account; $_SESSION["account"] = $account;
} }
public function setInitialTarget(?string $url): void {
$_SESSION["target"] = $url;
}
} }

@ -5,5 +5,7 @@ namespace App\Session;
use App\Data\Account; use App\Data\Account;
interface SessionHandle { interface SessionHandle {
public function getInitialTarget(): ?string;
public function getAccount(): ?Account; public function getAccount(): ?Account;
} }

Loading…
Cancel
Save