MAPG-69 redirect to sign up when user not found during login

This commit is contained in:
Bence Pőcze 2020-06-21 12:55:49 +02:00
parent 9697163457
commit 10b7766458
8 changed files with 78 additions and 22 deletions

View File

@ -10,10 +10,15 @@
MapGuesser.httpRequest('POST', form.action, function () { MapGuesser.httpRequest('POST', form.action, function () {
if (this.response.error) { if (this.response.error) {
if (this.response.error === 'user_not_found') {
window.location.replace('/signup');
return;
}
var errorText; var errorText;
switch (this.response.error) { switch (this.response.error) {
case 'user_not_found': case 'password_too_short':
errorText = 'No user found with the given email address. You can <a href="/signup" title="Sign up">sign up here</a>!'; errorText = 'The given password is too short. Please choose a password that is at least 6 characters long!'
break; break;
case 'user_not_active': case 'user_not_active':
errorText = 'User found with the given email address, but the account is not activated. Please check your email and click on the activation link!'; errorText = 'User found with the given email address, but the account is not activated. Please check your email and click on the activation link!';

View File

@ -25,7 +25,7 @@
case 'password_not_match': case 'password_not_match':
errorText = 'The given current password is wrong.' errorText = 'The given current password is wrong.'
break; break;
case 'passwords_too_short': case 'password_too_short':
errorText = 'The given new password is too short. Please choose a password that is at least 6 characters long!' errorText = 'The given new password is too short. Please choose a password that is at least 6 characters long!'
break; break;
case 'passwords_not_match': case 'passwords_not_match':

View File

@ -9,15 +9,13 @@
var formData = new FormData(form); var formData = new FormData(form);
MapGuesser.httpRequest('POST', form.action, function () { MapGuesser.httpRequest('POST', form.action, function () {
document.getElementById('loading').style.visibility = 'hidden';
if (this.response.error) { if (this.response.error) {
var errorText; var errorText;
switch (this.response.error) { switch (this.response.error) {
case 'email_not_valid': case 'email_not_valid':
errorText = 'The given email address is not valid.' errorText = 'The given email address is not valid.'
break; break;
case 'passwords_too_short': case 'password_too_short':
errorText = 'The given password is too short. Please choose a password that is at least 6 characters long!' errorText = 'The given password is too short. Please choose a password that is at least 6 characters long!'
break; break;
case 'passwords_not_match': case 'passwords_not_match':
@ -31,6 +29,8 @@
break; break;
} }
document.getElementById('loading').style.visibility = 'hidden';
var signupFormError = document.getElementById('signupFormError'); var signupFormError = document.getElementById('signupFormError');
signupFormError.style.display = 'block'; signupFormError.style.display = 'block';
signupFormError.innerHTML = errorText; signupFormError.innerHTML = errorText;
@ -38,10 +38,7 @@
return; return;
} }
document.getElementById('signupFormError').style.display = 'none'; window.location.replace('/signup/success');
form.reset();
MapGuesser.showModalWithContent('Sign up successful', 'Sign up was successful. Please check your email and click on the activation link to activate your account!');
}, formData); }, formData);
}; };
})(); })();

View File

@ -61,10 +61,23 @@ class LoginController
return new Redirect(\Container::$routeCollection->getRoute('index')->generateLink(), IRedirect::TEMPORARY); return new Redirect(\Container::$routeCollection->getRoute('index')->generateLink(), IRedirect::TEMPORARY);
} }
if ($this->request->session()->has('tmp_user_data')) {
$tmpUserData = $this->request->session()->get('tmp_user_data');
$data = ['email' => $tmpUserData['email']];
} else {
$data = []; $data = [];
}
return new HtmlContent('login/signup', $data); return new HtmlContent('login/signup', $data);
} }
public function getSignupSuccess()
{
$data = [];
return new HtmlContent('login/signup_success', $data);
}
public function getSignupWithGoogleForm() public function getSignupWithGoogleForm()
{ {
if ($this->request->user() !== null) { if ($this->request->user() !== null) {
@ -93,6 +106,16 @@ class LoginController
$user = $this->userRepository->getByEmail($this->request->post('email')); $user = $this->userRepository->getByEmail($this->request->post('email'));
if ($user === null) { if ($user === null) {
if (strlen($this->request->post('password')) < 6) {
$data = ['error' => 'password_too_short'];
return new JsonContent($data);
}
$tmpUser = new User();
$tmpUser->setPlainPassword($this->request->post('password'));
$this->request->session()->set('tmp_user_data', ['email' => $this->request->post('email'), 'password_hashed' => $tmpUser->getPassword()]);
$data = ['error' => 'user_not_found']; $data = ['error' => 'user_not_found'];
return new JsonContent($data); return new JsonContent($data);
} }
@ -183,8 +206,20 @@ class LoginController
return new JsonContent($data); return new JsonContent($data);
} }
if ($this->request->session()->has('tmp_user_data')) {
$tmpUserData = $this->request->session()->get('tmp_user_data');
$tmpUser = new User();
$tmpUser->setPassword($tmpUserData['password_hashed']);
if (!$tmpUser->checkPassword($this->request->post('password'))) {
$data = ['error' => 'passwords_not_match'];
return new JsonContent($data);
}
} else {
if (strlen($this->request->post('password')) < 6) { if (strlen($this->request->post('password')) < 6) {
$data = ['error' => 'passwords_too_short']; $data = ['error' => 'password_too_short'];
return new JsonContent($data); return new JsonContent($data);
} }
@ -192,6 +227,7 @@ class LoginController
$data = ['error' => 'passwords_not_match']; $data = ['error' => 'passwords_not_match'];
return new JsonContent($data); return new JsonContent($data);
} }
}
$user = new User(); $user = new User();
$user->setEmail($this->request->post('email')); $user->setEmail($this->request->post('email'));
@ -213,6 +249,8 @@ class LoginController
$this->sendConfirmationEmail($user->getEmail(), $token); $this->sendConfirmationEmail($user->getEmail(), $token);
$this->request->session()->delete('tmp_user_data');
$data = ['success' => true]; $data = ['success' => true];
return new JsonContent($data); return new JsonContent($data);
} }

View File

@ -52,7 +52,7 @@ class UserController implements ISecured
if (strlen($this->request->post('password_new')) > 0) { if (strlen($this->request->post('password_new')) > 0) {
if (strlen($this->request->post('password_new')) < 6) { if (strlen($this->request->post('password_new')) < 6) {
$data = ['error' => 'passwords_too_short']; $data = ['error' => 'password_too_short'];
return new JsonContent($data); return new JsonContent($data);
} }

View File

@ -9,9 +9,15 @@ $jsFiles = [
<h2>Sign up</h2> <h2>Sign up</h2>
<div class="box"> <div class="box">
<form id="signupForm" action="/signup" method="post"> <form id="signupForm" action="/signup" method="post">
<?php if (isset($email)): ?>
<p class="justify">No user found with the given email address. Sign up with one click!</p>
<input class="big fullWidth marginTop" type="email" name="email" placeholder="Email address" value="<?= $email ?>" required>
<input class="big fullWidth marginTop" type="password" name="password" placeholder="Password confirmation" required minlength="6" autofocus>
<?php else: ?>
<input class="big fullWidth" type="email" name="email" placeholder="Email address" required autofocus> <input class="big fullWidth" type="email" name="email" placeholder="Email address" required autofocus>
<input class="big fullWidth marginTop" type="password" name="password" placeholder="Password" required minlength="6"> <input class="big fullWidth marginTop" type="password" name="password" placeholder="Password" required minlength="6">
<input class="big fullWidth marginTop" type="password" name="password_confirm" placeholder="Password confirmation" required minlength="6"> <input class="big fullWidth marginTop" type="password" name="password_confirm" placeholder="Password confirmation" required minlength="6">
<?php endif; ?>
<p id="signupFormError" class="formError justify marginTop"></p> <p id="signupFormError" class="formError justify marginTop"></p>
<div class="right marginTop"> <div class="right marginTop">
<button type="submit">Sign up</button> <button type="submit">Sign up</button>

View File

@ -0,0 +1,9 @@
<?php require ROOT . '/views/templates/main_header.php'; ?>
<?php require ROOT . '/views/templates/header.php'; ?>
<div class="main">
<h2>Sign up</h2>
<div class="box">
<p class="justify">Sign up was successful. Please check your email and click on the activation link to activate your account!</p>
</div>
</div>
<?php require ROOT . '/views/templates/main_footer.php'; ?>

View File

@ -21,6 +21,7 @@ Container::$routeCollection->get('signup', 'signup', [MapGuesser\Controller\Logi
Container::$routeCollection->post('signup-action', 'signup', [MapGuesser\Controller\LoginController::class, 'signup']); Container::$routeCollection->post('signup-action', 'signup', [MapGuesser\Controller\LoginController::class, 'signup']);
Container::$routeCollection->get('signup-google', 'signup/google', [MapGuesser\Controller\LoginController::class, 'getSignupWithGoogleForm']); Container::$routeCollection->get('signup-google', 'signup/google', [MapGuesser\Controller\LoginController::class, 'getSignupWithGoogleForm']);
Container::$routeCollection->post('signup-google-action', 'signup/google', [MapGuesser\Controller\LoginController::class, 'signupWithGoogle']); Container::$routeCollection->post('signup-google-action', 'signup/google', [MapGuesser\Controller\LoginController::class, 'signupWithGoogle']);
Container::$routeCollection->get('signup.success', 'signup/success', [MapGuesser\Controller\LoginController::class, 'getSignupSuccess']);
Container::$routeCollection->get('signup.activate', 'signup/activate/{token}', [MapGuesser\Controller\LoginController::class, 'activate']); Container::$routeCollection->get('signup.activate', 'signup/activate/{token}', [MapGuesser\Controller\LoginController::class, 'activate']);
Container::$routeCollection->get('signup.cancel', 'signup/cancel/{token}', [MapGuesser\Controller\LoginController::class, 'cancel']); Container::$routeCollection->get('signup.cancel', 'signup/cancel/{token}', [MapGuesser\Controller\LoginController::class, 'cancel']);
Container::$routeCollection->get('logout', 'logout', [MapGuesser\Controller\LoginController::class, 'logout']); Container::$routeCollection->get('logout', 'logout', [MapGuesser\Controller\LoginController::class, 'logout']);