Finish registration, add validaton rules to Player
This commit is contained in:
parent
81670044f3
commit
a14804ff5f
|
@ -30,7 +30,8 @@ body {
|
||||||
align-items: center;
|
align-items: center;
|
||||||
background-color: rgba(0, 0, 0, 0.25);
|
background-color: rgba(0, 0, 0, 0.25);
|
||||||
padding: 0.5rem;
|
padding: 0.5rem;
|
||||||
color: white;
|
color: rgba(0, 0, 0, 0.75);
|
||||||
|
font-size: 0.75rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
div#content {
|
div#content {
|
||||||
|
|
|
@ -52,4 +52,22 @@ class Player
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function goodUsername(string $username): bool
|
||||||
|
{
|
||||||
|
// username must be alphanumeric and between 2 and 20 characters, allow single spaces
|
||||||
|
return preg_match('/^(?!.* )[a-zA-Z0-9 ]{2,20}$/', $username);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function uniqueUsername(string $username): bool
|
||||||
|
{
|
||||||
|
$player = App::$db->do("SELECT id FROM players WHERE LOWER(username) = :i LIMIT 1;", ['i' => strtolower($username)]);
|
||||||
|
return $player->fetch() == false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function uniqueEmail(string $email): bool
|
||||||
|
{
|
||||||
|
$player = App::$db->do("SELECT id FROM players WHERE LOWER(email) = :i LIMIT 1;", ['i' => strtolower($email)]);
|
||||||
|
return $player->fetch() == false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ class GateModule
|
||||||
|
|
||||||
if ($s == '' || $s == 'login') return self::login($m);
|
if ($s == '' || $s == 'login') return self::login($m);
|
||||||
if ($s == 'logout' && $m == 'POST') return self::logout();
|
if ($s == 'logout' && $m == 'POST') return self::logout();
|
||||||
|
if ($s == 'register') return self::register($m);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function login(string $method)
|
public static function login(string $method)
|
||||||
|
@ -52,4 +53,69 @@ class GateModule
|
||||||
App::flash('success', 'You have been logged out.');
|
App::flash('success', 'You have been logged out.');
|
||||||
redirect('/');
|
redirect('/');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static function register(string $method)
|
||||||
|
{
|
||||||
|
// just display the register page
|
||||||
|
if ($method == 'GET') {
|
||||||
|
echo render('layout', ['title' => 'Register', 'content' => 'gate/register']);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle the register form
|
||||||
|
$un = trim($_POST['username'] ?? '');
|
||||||
|
$em = trim($_POST['email'] ?? '');
|
||||||
|
$pw = $_POST['password'] ?? '';
|
||||||
|
$pw2 = $_POST['password2'] ?? '';
|
||||||
|
$cl = $_POST['class'] ?? 1;
|
||||||
|
|
||||||
|
$errors = [];
|
||||||
|
|
||||||
|
// fields are required
|
||||||
|
if (empty($un)) $errors['un'] = 'Please enter a username.';
|
||||||
|
if (empty($em)) $errors['em'] = 'Please enter an email address.';
|
||||||
|
if (empty($pw)) $errors['pw'] = 'Please enter a password.';
|
||||||
|
if (empty($pw2)) $errors['pw2'] = 'Please confirm your password.';
|
||||||
|
|
||||||
|
if (!empty($errors)) {
|
||||||
|
App::flash('errors', $errors);
|
||||||
|
redirect('/gate/register');
|
||||||
|
}
|
||||||
|
|
||||||
|
// password must be at least 6 characters
|
||||||
|
if (strlen($pw) < 6) $errors['pw'] = 'Password must be at least 6 characters.';
|
||||||
|
|
||||||
|
// passwords must match
|
||||||
|
if ($pw != $pw2) $errors['pw2'] = 'Passwords do not match.';
|
||||||
|
|
||||||
|
// email address must be valid format
|
||||||
|
if (!filter_var($em, FILTER_VALIDATE_EMAIL)) $errors['em'] = 'Invalid email address.';
|
||||||
|
|
||||||
|
// username must be alphanumeric and between 2 and 20 characters, allow single spaces
|
||||||
|
if (!Player::goodUsername($un)) $errors['un'] = 'Invalid username. Must be alphanumeric and between 2 and 20 characters, and can contain spaces.';
|
||||||
|
|
||||||
|
// username must be unique
|
||||||
|
if (!Player::uniqueUsername($un)) $errors['un'] = 'Username already exists.';
|
||||||
|
|
||||||
|
// email address must be unique
|
||||||
|
if (!Player::uniqueEmail($em)) $errors['em'] = 'Email address already exists.';
|
||||||
|
|
||||||
|
// flash errors and redirect back to form
|
||||||
|
if (!empty($errors)) {
|
||||||
|
App::flash('errors', $errors);
|
||||||
|
redirect('/gate/register');
|
||||||
|
}
|
||||||
|
|
||||||
|
// create the player
|
||||||
|
Player::create([
|
||||||
|
'username' => $un,
|
||||||
|
'email' => $em,
|
||||||
|
'password' => password_hash($pw, PASSWORD_ARGON2ID),
|
||||||
|
'class_id' => $cl
|
||||||
|
]);
|
||||||
|
|
||||||
|
// redirect to login
|
||||||
|
App::flash('success', "You're now an adventurer! Go forth, $un!");
|
||||||
|
redirect('/gate/login');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
18
server/templates/gate/register.php
Normal file
18
server/templates/gate/register.php
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
<?php
|
||||||
|
if (App::flash('errors')) {
|
||||||
|
print_r(App::flash('errors'));
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
|
||||||
|
<form action="/gate/register" method="post">
|
||||||
|
<input type="text" name="username" placeholder="Username">
|
||||||
|
<input type="text" name="email" placeholder="Email">
|
||||||
|
<input type="password" name="password" placeholder="Password">
|
||||||
|
<input type="password" name="password2" placeholder="Confirm Password">
|
||||||
|
<select name="class">
|
||||||
|
<?php foreach (Classes::all() as $class): ?>
|
||||||
|
<option value="<?= $class['id'] ?>"><?= $class['name'] ?></option>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</select>
|
||||||
|
<button>Register</button>
|
||||||
|
</form>
|
Loading…
Reference in New Issue
Block a user