Finish initial auth, rename app/ class files for consistency

This commit is contained in:
Sky Johnson 2024-07-15 19:54:50 -05:00
parent 5d185cf0a1
commit 506f1a44d1
10 changed files with 48 additions and 17 deletions

View File

@ -9,6 +9,7 @@ class App
public static Database $db;
private static string $dbPath;
public static Request $req;
public static Auth $auth;
public static array $s = []; // game settings
public function __construct(string $dbPath)
@ -20,6 +21,9 @@ class App
// load game settings
$s = self::$db->q('SELECT * FROM settings WHERE id = 1;');
self::$s = $s ? $s->fetch() : [];
// init authentication
self::$auth = new Auth();
}
public static function performDatabaseReset(): void
@ -29,4 +33,9 @@ class App
self::$db = new Database(self::$dbPath);
}
}
public static function auth(): bool
{
return self::$auth->good();
}
}

View File

@ -13,10 +13,15 @@ class Auth
// id of the player
public static int $id = 0;
public static function login(string $identifier, string $password, bool $remember = false): bool
public function __construct()
{
$this->good();
}
public function login(string $identifier, string $password, bool $remember = false): bool
{
// delete the old session
if (isset($_SESSION['player_id'])) self::logout();
if (isset($_SESSION['player_id'])) $this->logout();
// get the player by their username
$id = Player::validateCredentials($identifier, $password);
@ -27,12 +32,12 @@ class Auth
self::$id = $id;
// set the remember me cookie
if ($remember) self::remember($id);
if ($remember) $this->remember($id);
return true;
}
private static function remember(int $id): array|false
private function remember(int $id): array|false
{
$data = ['player_id' => $id, 'token' => token()];
@ -42,14 +47,13 @@ class Auth
return $data;
}
private static function logout(): void
private function logout(): void
{
if (isset($_SESSION['player_id'])) unset($_SESSION['player_id']);
if (isset($_SESSION['remember'])) unset($_SESSION['remember']);
if (isset($_COOKIE[self::COOKIE_NAME])) setcookie(self::COOKIE_NAME, '', time() - 86400, '/', '', true, true);
}
public static function good(): bool
public function good(): bool
{
// if our player_id session still exists, carry on
if (isset($_SESSION['player_id'])) {
@ -65,7 +69,7 @@ class Auth
if (!Session::validate($cookie[0], $cookie[1])) return false; // the token is invalid
// token is valid, refresh cookie and assign session
self::remember($cookie[0]);
$this->remember($cookie[0]);
$_SESSION['player_id'] = $cookie[0];
self::$id = $cookie[0];
return true;

View File

@ -39,18 +39,21 @@ const MAP = [
// 'Class' => 'path/to/class.php',
// server-level classes
'App' => SERVER.'/app/app.php',
'Database' => SERVER.'/app/database.php',
'Request' => SERVER.'/app/request.php',
'App' => SERVER.'/app/App.php',
'Database' => SERVER.'/app/Database.php',
'Request' => SERVER.'/app/Request.php',
'Auth' => SERVER.'/app/Auth.php',
// modules
'HomeModule' => SERVER.'/modules/HomeModule.php',
'InstallModule' => SERVER.'/modules/InstallModule.php',
'GateModule' => SERVER.'/modules/GateModule.php',
// models
'Classes' => SERVER.'/models/Classes.php',
'Player' => SERVER.'/models/Player.php',
'Spell' => SERVER.'/models/Spell.php',
'Session' => SERVER.'/models/Session.php',
];
// autoloader

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 username = :i OR email = :i LIMIT 1;", ['i' => $identifier]);
if ($player == false) return false;
$player = $player->fetch();

View File

@ -0,0 +1,6 @@
<?php
class GateModule
{
}

View File

@ -4,7 +4,12 @@ class HomeModule
{
public static function home()
{
echo 'Welcome to the home module!';
if (App::auth()) {
echo 'You are already logged in!<br>';
} else {
echo 'You are not logged in!<br>';
}
echo 'Your request is: ' . App::$req->uri(0);
}
}

View File

@ -294,11 +294,15 @@ class InstallModule
'level' => $_POST['level'] ?? 1
]);
// Create the .installed file in the server folder
file_put_contents(SERVER.'/.installed', 'Installed on '.date('Y-m-d H:i:s'));
// login the admin
App::$auth->login($_POST['username'], $_POST['password']);
// Render the finished page!
echo render('install/layout', ['title' => 'Finished!', 'step' => 'done', 'name' => $_POST['username'], 'complete' => $_POST['complete'] ?? false]);
// Create the .installed file in the server folder
file_put_contents(SERVER.'/.installed', 'Installed on '.date('Y-m-d H:i:s'));
}
private static function fourOhFour()

View File

@ -1,12 +1,12 @@
<p class="mb-1">
Congratulations, <?= $name ?>! Your installation is complete. Dragon Knight is ready to go.
All that's left is to log in and start playing. <?php if (!$complete): ?>Once you've logged in,
All that's left is to start playing. <?php if (!$complete): ?>Once you've logged in,
you can create some classes and assign your character one. By default you are a useless Adventurer.
😜<?php endif; ?>
</p>
<p class="mb-1">
<a href="/gate/login">Click here to log in.</a>
<a href="/gate/login">Click here to begin your adventure.</a>
</p>
<p>