56 lines
1.3 KiB
PHP
56 lines
1.3 KiB
PHP
<?php
|
|
|
|
class GateModule
|
|
{
|
|
private const GUEST = ['login', 'register'];
|
|
|
|
public static function handle()
|
|
{
|
|
$s = App::$req->uri(1) ?? ''; // second segment
|
|
$m = App::$req->method; // request method
|
|
|
|
if (App::auth() && in_array($s, self::GUEST)) redirect('/');
|
|
|
|
if ($s == '' || $s == 'login') return self::login($m);
|
|
if ($s == 'logout' && $m == 'POST') return self::logout();
|
|
}
|
|
|
|
public static function login(string $method)
|
|
{
|
|
// just display the login page
|
|
if ($method == 'GET') {
|
|
echo render('layout', ['title' => 'Login', 'content' => 'gate/login']);
|
|
return;
|
|
}
|
|
|
|
// handle the login form
|
|
$id = trim($_POST['id'] ?? ''); // identifier; let a user log in with email or username
|
|
$pw = $_POST['pw'] ?? ''; // password
|
|
|
|
// fields are required
|
|
if (empty($id) || empty($pw)) {
|
|
App::flash('error', 'Please fill out all fields.');
|
|
redirect('/gate/login');
|
|
}
|
|
|
|
// find the user, login if valid
|
|
$found = App::$auth->login($id, $pw, isset($_POST['remember']));
|
|
|
|
// Login is valid!
|
|
if ($found) {
|
|
App::flash('success', 'Welcome back!');
|
|
redirect('/');
|
|
} else {
|
|
App::flash('error', 'Player account not found.');
|
|
redirect('/gate/login');
|
|
}
|
|
}
|
|
|
|
private static function logout()
|
|
{
|
|
App::$auth->logout();
|
|
App::flash('success', 'You have been logged out.');
|
|
redirect('/');
|
|
}
|
|
}
|