Add flash messages, work on layout

This commit is contained in:
Sky Johnson 2024-07-17 12:51:50 -05:00
parent 56524da0c6
commit 167f28ff2c
4 changed files with 64 additions and 15 deletions

View File

@ -11,6 +11,7 @@ class App
public static Request $req;
public static Auth $auth;
public static array $s = []; // game settings
public static array $flashes = []; // flash messages
public function __construct(string $dbPath)
{
@ -18,12 +19,18 @@ class App
self::$db = new Database($dbPath); // the database
self::$dbPath = $dbPath; // the database path
// load game settings
$s = self::$db->q('SELECT * FROM settings WHERE id = 1;');
self::$s = $s ? $s->fetch() : [];
// stuff that can only be loaded if the database is installed
if (INSTALLED) {
// load game settings
$s = self::$db->q('SELECT * FROM settings WHERE id = 1;');
self::$s = $s ? $s->fetch() : [];
// init authentication
self::$auth = new Auth();
// init authentication
self::$auth = new Auth();
}
// load flash messages
self::$flashes = $_SESSION['flash'] ?? [];
}
public static function performDatabaseReset(): void
@ -38,4 +45,20 @@ class App
{
return self::$auth->good();
}
public static function flash(string $key, mixed $value = null): mixed
{
// get a flash message
if ($value === null) return self::$flashes[$key] ?? null;
// set a flash message
$_SESSION['flash'][$key] = $value;
self::$flashes[$key] = $value;
}
public function __destruct()
{
// clean up flash messages
$_SESSION['flash'] = [];
}
}

View File

@ -40,7 +40,7 @@ class Player
public static function validateCredentials(string $identifier, string $password, bool $fetch = false): int|false
{
// get the player from their username or email
$player = App::$db->do("SELECT " . ($fetch ? '*' : 'id, password') . " FROM players WHERE username = :i OR email = :i LIMIT 1;", ['i' => $identifier]);
$player = App::$db->do("SELECT " . ($fetch ? '*' : 'id, password') . " FROM players WHERE LOWER(username) = :i OR LOWER(email) = :i LIMIT 1;", ['i' => strtolower($identifier)]);
if ($player == false) return false;
$player = $player->fetch();
@ -52,4 +52,4 @@ class Player
return false;
}
}
}

View File

@ -9,11 +9,37 @@ class GateModule
$s = App::$req->uri(1) ?? ''; // second segment
$m = App::$req->method; // request method
if ($s == '' || $s == 'login') return self::login();
if ($s == '' || $s == 'login') return self::login($m);
}
public static function login()
public static function login(string $method)
{
echo render('layout', ['title' => 'Login']);
// 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');
}
}
}
}

View File

@ -16,19 +16,19 @@
<aside id="left">
left
</aside>
<main>
<main>
<?= render($content, $data) ?>
</main>
<aside id="right">
right
</aside>
</div>
<footer>
<?= render('partials/footer', $data) ?>
</footer>
</div>
</body>
</html>
</html>