From 364d55a4b27b05d4d29a9ff9e7b22479f2341384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sat, 8 Apr 2023 19:07:18 +0200 Subject: [PATCH] RVRNEXT-2 fix redirect after login --- src/Controller/LoginController.php | 35 +++++++++++++++--------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/Controller/LoginController.php b/src/Controller/LoginController.php index 8588ebf..9c2445b 100644 --- a/src/Controller/LoginController.php +++ b/src/Controller/LoginController.php @@ -27,21 +27,27 @@ class LoginController private UserPasswordResetterRepository $userPasswordResetterRepository; + private string $redirectUrl; + public function __construct(IRequest $request) { $this->request = $request; $this->pdm = new PersistentDataManager(); $this->userRepository = new UserRepository(); $this->userPasswordResetterRepository = new UserPasswordResetterRepository(); + $this->redirectUrl = $this->request->session()->has('redirect_after_login') ? + $this->request->session()->get('redirect_after_login') : + \Container::$routeCollection->getRoute('index')->generateLink(); } public function getLoginForm() { if ($this->request->user() !== null) { - return new Redirect(\Container::$routeCollection->getRoute('index')->generateLink(), IRedirect::TEMPORARY); + $this->deleteRedirectUrl(); + return new Redirect($this->redirectUrl, IRedirect::TEMPORARY); } - return new HtmlContent('login/login', ['redirectUrl' => $this->getRedirectUrl()]); + return new HtmlContent('login/login', ['redirectUrl' => $this->redirectUrl]); } public function getGoogleLoginRedirect(): IRedirect @@ -65,7 +71,8 @@ class LoginController public function getRequestPasswordResetForm() { if ($this->request->user() !== null) { - return new Redirect(\Container::$routeCollection->getRoute('index')->generateLink(), IRedirect::TEMPORARY); + $this->deleteRedirectUrl(); + return new Redirect($this->redirectUrl, IRedirect::TEMPORARY); } return new HtmlContent('login/password_reset_request', ['email' => $this->request->query('email')]); @@ -79,7 +86,8 @@ class LoginController public function getResetPasswordForm() { if ($this->request->user() !== null) { - return new Redirect(\Container::$routeCollection->getRoute('index')->generateLink(), IRedirect::TEMPORARY); + $this->deleteRedirectUrl(); + return new Redirect($this->redirectUrl, IRedirect::TEMPORARY); } $token = $this->request->query('token'); @@ -91,7 +99,7 @@ class LoginController $user = $this->userRepository->getById($resetter->getUserId()); - return new HtmlContent('login/reset_password', ['success' => true, 'token' => $token, 'email' => $user->getEmail(), 'redirectUrl' => $this->getRedirectUrl()]); + return new HtmlContent('login/reset_password', ['success' => true, 'token' => $token, 'email' => $user->getEmail(), 'redirectUrl' => $this->redirectUrl]); } public function login(): IContent @@ -123,7 +131,7 @@ class LoginController if ($this->request->user() !== null) { $this->deleteRedirectUrl(); - return new Redirect($this->getRedirectUrl(), IRedirect::TEMPORARY); + return new Redirect($this->redirectUrl, IRedirect::TEMPORARY); } if ($this->request->query('state') !== $this->request->session()->get('oauth_state')) { @@ -159,7 +167,7 @@ class LoginController $this->request->setUser($user); $this->deleteRedirectUrl(); - return new Redirect($this->getRedirectUrl(), IRedirect::TEMPORARY); + return new Redirect($this->redirectUrl, IRedirect::TEMPORARY); } public function logout(): IRedirect @@ -175,7 +183,7 @@ class LoginController $this->deleteRedirectUrl(); return new JsonContent([ 'redirect' => [ - 'target' => $this->getRedirectUrl() + 'target' => $this->redirectUrl ] ]); } @@ -239,7 +247,7 @@ class LoginController $this->deleteRedirectUrl(); return new JsonContent([ 'redirect' => [ - 'target' => $this->getRedirectUrl() + 'target' => $this->redirectUrl ] ]); } @@ -298,15 +306,6 @@ class LoginController $mail->send(); } - private function getRedirectUrl(): string - { - $redirectUrl = $this->request->session()->get('redirect_after_login'); - if ($redirectUrl === null) { - return \Container::$routeCollection->getRoute('index')->generateLink(); - } - return $redirectUrl; - } - private function deleteRedirectUrl(): void { $this->request->session()->delete('redirect_after_login');