diff --git a/public/index.php b/public/index.php index f09c63c..0847726 100644 --- a/public/index.php +++ b/public/index.php @@ -19,3 +19,6 @@ if ($route == 'gate') return GateModule::handle(); // 404 http_response_code(404); echo '404: ' . $route; + +// cleanup +$app->cleanup(); diff --git a/server/app/App.php b/server/app/App.php index 88e90d0..4fa657f 100644 --- a/server/app/App.php +++ b/server/app/App.php @@ -20,12 +20,11 @@ class App self::$dbPath = $dbPath; // the database path self::$auth = new Auth(); - // 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() : []; + // load game settings + $s = self::$db->q('SELECT * FROM settings WHERE id = 1;'); + self::$s = $s ? $s->fetch() : []; + if (INSTALLED) { // load the player's auth self::$auth->good(); } @@ -57,9 +56,10 @@ class App self::$flashes[$key] = $value; } - public function __destruct() + public function cleanup() { // clean up flash messages + $_SESSION['flash'] = []; unset($_SESSION['flash']); } } diff --git a/server/app/Auth.php b/server/app/Auth.php index df82044..6d2f49c 100644 --- a/server/app/Auth.php +++ b/server/app/Auth.php @@ -47,7 +47,7 @@ class Auth return $data; } - private function logout(): void + public function logout(): void { if (isset($_SESSION['player_id'])) unset($_SESSION['player_id']); if (isset($_COOKIE[self::COOKIE_NAME])) setcookie(self::COOKIE_NAME, '', time() - 86400, '/', '', true, true); diff --git a/server/modules/GateModule.php b/server/modules/GateModule.php index c036187..962b986 100644 --- a/server/modules/GateModule.php +++ b/server/modules/GateModule.php @@ -9,7 +9,10 @@ class GateModule $s = App::$req->uri(1) ?? ''; // second segment $m = App::$req->method; // request method + if (App::$auth->good() && 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) @@ -42,4 +45,11 @@ class GateModule redirect('/gate/login'); } } + + private static function logout() + { + App::$auth->logout(); + App::flash('success', 'You have been logged out.'); + redirect('/'); + } } diff --git a/server/modules/HomeModule.php b/server/modules/HomeModule.php index d576b96..78236a7 100644 --- a/server/modules/HomeModule.php +++ b/server/modules/HomeModule.php @@ -4,8 +4,13 @@ class HomeModule { public static function home() { + foreach ($_SESSION['flash'] as $key => $value) { + echo '