unify redirect after login logic
All checks were successful
rvr-nextgen/pipeline/pr-master This commit looks good

This commit is contained in:
Bence Pőcze 2023-04-08 10:45:56 +02:00
parent d45b790122
commit 453940a5ef
Signed by: bence
GPG Key ID: DC5BD6E95A333E6D
4 changed files with 30 additions and 15 deletions

View File

@ -29,7 +29,7 @@ if ($match !== null) {
}
if (!$authorized) {
Container::$request->session()->set('redirect_after_login', $url);
Container::$request->session()->set('redirect_after_login', '/' . $url);
$response = new Redirect(Container::$routeCollection->getRoute('login')->generateLink(), IRedirect::TEMPORARY);
header('Location: ' . $response->getUrl(), true, $response->getHttpCode());
return;

View File

@ -41,7 +41,7 @@ class LoginController
return new Redirect(\Container::$routeCollection->getRoute('index')->generateLink(), IRedirect::TEMPORARY);
}
return new HtmlContent('login/login');
return new HtmlContent('login/login', ['redirectUrl' => $this->getRedirectUrl()]);
}
public function getGoogleLoginRedirect(): IRedirect
@ -91,12 +91,13 @@ class LoginController
$user = $this->userRepository->getById($resetter->getUserId());
return new HtmlContent('login/reset_password', ['success' => true, 'token' => $token, 'email' => $user->getEmail()]);
return new HtmlContent('login/reset_password', ['success' => true, 'token' => $token, 'email' => $user->getEmail(), 'redirectUrl' => $this->getRedirectUrl()]);
}
public function login(): IContent
{
if ($this->request->user() !== null) {
$this->deleteRedirectUrl();
return new JsonContent(['success' => true]);
}
@ -112,20 +113,17 @@ class LoginController
$this->request->setUser($user);
$this->deleteRedirectUrl();
return new JsonContent(['success' => true]);
}
public function loginWithGoogle()
{
$redirectUrl = $this->request->session()->get('redirect_after_login');
if ($redirectUrl === null) {
$redirectUrl = \Container::$routeCollection->getRoute('index')->generateLink();
}
$defaultError = 'Authentication with Google failed. Please <a href="/login/google" title="Login with Google">try again</a>!';
if ($this->request->user() !== null) {
$this->request->session()->delete('redirect_after_login');
return new Redirect($redirectUrl, IRedirect::TEMPORARY);
$this->deleteRedirectUrl();
return new Redirect($this->getRedirectUrl(), IRedirect::TEMPORARY);
}
if ($this->request->query('state') !== $this->request->session()->get('oauth_state')) {
@ -160,8 +158,8 @@ class LoginController
$this->request->setUser($user);
$this->request->session()->delete('redirect_after_login');
return new Redirect($redirectUrl, IRedirect::TEMPORARY);
$this->deleteRedirectUrl();
return new Redirect($this->getRedirectUrl(), IRedirect::TEMPORARY);
}
public function logout(): IRedirect
@ -174,9 +172,10 @@ class LoginController
public function requestPasswordReset(): IContent
{
if ($this->request->user() !== null) {
$this->deleteRedirectUrl();
return new JsonContent([
'redirect' => [
'target' => '/' . \Container::$routeCollection->getRoute('home')->generateLink()
'target' => $this->getRedirectUrl()
]
]);
}
@ -237,9 +236,10 @@ class LoginController
public function resetPassword(): IContent
{
if ($this->request->user() !== null) {
$this->deleteRedirectUrl();
return new JsonContent([
'redirect' => [
'target' => '/' . \Container::$routeCollection->getRoute('home')->generateLink()
'target' => $this->getRedirectUrl()
]
]);
}
@ -280,6 +280,7 @@ class LoginController
$this->request->setUser($user);
$this->deleteRedirectUrl();
return new JsonContent(['success' => true]);
}
@ -296,4 +297,18 @@ 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');
}
}

View File

@ -3,7 +3,7 @@
@section(main)
<h2>Login</h2>
<div class="box">
<form id="loginForm" action="/login" method="post" data-redirect-on-success="/">
<form id="loginForm" action="/login" method="post" data-redirect-on-success="<?= $redirectUrl ?>">
<input type="email" class="text big fullWidth" name="email" placeholder="Email address" required autofocus>
<input type="password" class="text big fullWidth marginTop" name="password" placeholder="Password" required minlength="6">
<p id="loginFormError" class="formError justify marginTop"></p>

View File

@ -4,7 +4,7 @@
<h2>Reset password</h2>
<div class="box">
<?php if ($success) : ?>
<form id="resetPasswordForm" action="/password/reset/<?= $token ?>" method="post" data-redirect-on-success="/">
<form id="resetPasswordForm" action="/password/reset/<?= $token ?>" method="post" data-redirect-on-success="<?= $redirectUrl ?>">
<input type="email" class="text big fullWidth" name="email" placeholder="Email address" value="<?= $email ?>" disabled>
<input type="password" class="text big fullWidth marginTop" name="password" placeholder="Password" required minlength="6" autofocus>
<input type="password" class="text big fullWidth marginTop" name="password_confirm" placeholder="Password confirmation" required minlength="6">