Finish initial auth, rename app/ class files for consistency
This commit is contained in:
parent
5d185cf0a1
commit
506f1a44d1
|
@ -9,6 +9,7 @@ class App
|
||||||
public static Database $db;
|
public static Database $db;
|
||||||
private static string $dbPath;
|
private static string $dbPath;
|
||||||
public static Request $req;
|
public static Request $req;
|
||||||
|
public static Auth $auth;
|
||||||
public static array $s = []; // game settings
|
public static array $s = []; // game settings
|
||||||
|
|
||||||
public function __construct(string $dbPath)
|
public function __construct(string $dbPath)
|
||||||
|
@ -20,6 +21,9 @@ class App
|
||||||
// load game settings
|
// load game settings
|
||||||
$s = self::$db->q('SELECT * FROM settings WHERE id = 1;');
|
$s = self::$db->q('SELECT * FROM settings WHERE id = 1;');
|
||||||
self::$s = $s ? $s->fetch() : [];
|
self::$s = $s ? $s->fetch() : [];
|
||||||
|
|
||||||
|
// init authentication
|
||||||
|
self::$auth = new Auth();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function performDatabaseReset(): void
|
public static function performDatabaseReset(): void
|
||||||
|
@ -29,4 +33,9 @@ class App
|
||||||
self::$db = new Database(self::$dbPath);
|
self::$db = new Database(self::$dbPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function auth(): bool
|
||||||
|
{
|
||||||
|
return self::$auth->good();
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -13,10 +13,15 @@ class Auth
|
||||||
// id of the player
|
// id of the player
|
||||||
public static int $id = 0;
|
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
|
// delete the old session
|
||||||
if (isset($_SESSION['player_id'])) self::logout();
|
if (isset($_SESSION['player_id'])) $this->logout();
|
||||||
|
|
||||||
// get the player by their username
|
// get the player by their username
|
||||||
$id = Player::validateCredentials($identifier, $password);
|
$id = Player::validateCredentials($identifier, $password);
|
||||||
|
@ -27,12 +32,12 @@ class Auth
|
||||||
self::$id = $id;
|
self::$id = $id;
|
||||||
|
|
||||||
// set the remember me cookie
|
// set the remember me cookie
|
||||||
if ($remember) self::remember($id);
|
if ($remember) $this->remember($id);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function remember(int $id): array|false
|
private function remember(int $id): array|false
|
||||||
{
|
{
|
||||||
$data = ['player_id' => $id, 'token' => token()];
|
$data = ['player_id' => $id, 'token' => token()];
|
||||||
|
|
||||||
|
@ -42,14 +47,13 @@ class Auth
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function logout(): void
|
private function logout(): void
|
||||||
{
|
{
|
||||||
if (isset($_SESSION['player_id'])) unset($_SESSION['player_id']);
|
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);
|
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 our player_id session still exists, carry on
|
||||||
if (isset($_SESSION['player_id'])) {
|
if (isset($_SESSION['player_id'])) {
|
||||||
|
@ -65,7 +69,7 @@ class Auth
|
||||||
if (!Session::validate($cookie[0], $cookie[1])) return false; // the token is invalid
|
if (!Session::validate($cookie[0], $cookie[1])) return false; // the token is invalid
|
||||||
|
|
||||||
// token is valid, refresh cookie and assign session
|
// token is valid, refresh cookie and assign session
|
||||||
self::remember($cookie[0]);
|
$this->remember($cookie[0]);
|
||||||
$_SESSION['player_id'] = $cookie[0];
|
$_SESSION['player_id'] = $cookie[0];
|
||||||
self::$id = $cookie[0];
|
self::$id = $cookie[0];
|
||||||
return true;
|
return true;
|
|
@ -39,18 +39,21 @@ const MAP = [
|
||||||
// 'Class' => 'path/to/class.php',
|
// 'Class' => 'path/to/class.php',
|
||||||
|
|
||||||
// server-level classes
|
// server-level classes
|
||||||
'App' => SERVER.'/app/app.php',
|
'App' => SERVER.'/app/App.php',
|
||||||
'Database' => SERVER.'/app/database.php',
|
'Database' => SERVER.'/app/Database.php',
|
||||||
'Request' => SERVER.'/app/request.php',
|
'Request' => SERVER.'/app/Request.php',
|
||||||
|
'Auth' => SERVER.'/app/Auth.php',
|
||||||
|
|
||||||
// modules
|
// modules
|
||||||
'HomeModule' => SERVER.'/modules/HomeModule.php',
|
'HomeModule' => SERVER.'/modules/HomeModule.php',
|
||||||
'InstallModule' => SERVER.'/modules/InstallModule.php',
|
'InstallModule' => SERVER.'/modules/InstallModule.php',
|
||||||
|
'GateModule' => SERVER.'/modules/GateModule.php',
|
||||||
|
|
||||||
// models
|
// models
|
||||||
'Classes' => SERVER.'/models/Classes.php',
|
'Classes' => SERVER.'/models/Classes.php',
|
||||||
'Player' => SERVER.'/models/Player.php',
|
'Player' => SERVER.'/models/Player.php',
|
||||||
'Spell' => SERVER.'/models/Spell.php',
|
'Spell' => SERVER.'/models/Spell.php',
|
||||||
|
'Session' => SERVER.'/models/Session.php',
|
||||||
];
|
];
|
||||||
|
|
||||||
// autoloader
|
// autoloader
|
||||||
|
|
|
@ -40,7 +40,7 @@ class Player
|
||||||
public static function validateCredentials(string $identifier, string $password, bool $fetch = false): int|false
|
public static function validateCredentials(string $identifier, string $password, bool $fetch = false): int|false
|
||||||
{
|
{
|
||||||
// get the player from their username or email
|
// 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;
|
if ($player == false) return false;
|
||||||
$player = $player->fetch();
|
$player = $player->fetch();
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class GateModule
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -4,7 +4,12 @@ class HomeModule
|
||||||
{
|
{
|
||||||
public static function home()
|
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);
|
echo 'Your request is: ' . App::$req->uri(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -294,11 +294,15 @@ class InstallModule
|
||||||
'level' => $_POST['level'] ?? 1
|
'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!
|
// Render the finished page!
|
||||||
echo render('install/layout', ['title' => 'Finished!', 'step' => 'done', 'name' => $_POST['username'], 'complete' => $_POST['complete'] ?? false]);
|
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()
|
private static function fourOhFour()
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
<p class="mb-1">
|
<p class="mb-1">
|
||||||
Congratulations, <?= $name ?>! Your installation is complete. Dragon Knight is ready to go.
|
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.
|
you can create some classes and assign your character one. By default you are a useless Adventurer.
|
||||||
😜<?php endif; ?>
|
😜<?php endif; ?>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p class="mb-1">
|
<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>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user