Dragon-Knight/server/modules/GateModule.php

46 lines
1.0 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 ($s == '' || $s == 'login') return self::login($m);
}
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');
}
}
}