From a20c443178e1fe661c69fe01c0874e7dc603af5f Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Wed, 17 Jul 2024 18:41:34 -0500 Subject: [PATCH] working on login --- public/index.php | 3 +++ server/app/App.php | 12 ++++++------ server/app/Auth.php | 2 +- server/modules/GateModule.php | 10 ++++++++++ server/modules/HomeModule.php | 5 +++++ server/modules/InstallModule.php | 7 +++---- server/templates/gate/login.php | 8 +++++++- server/templates/layout.php | 8 ++++++-- server/templates/partials/left.php | 1 + server/templates/partials/right.php | 1 + 10 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 server/templates/partials/left.php create mode 100644 server/templates/partials/right.php 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 '
- ' . $value . '
'; + } + if (App::auth()) { echo 'You are already logged in!
'; + echo '
'; } else { echo 'You are not logged in!
'; } diff --git a/server/modules/InstallModule.php b/server/modules/InstallModule.php index ad9eea9..d3f2a4f 100644 --- a/server/modules/InstallModule.php +++ b/server/modules/InstallModule.php @@ -254,9 +254,6 @@ class InstallModule 'expires' DATETIME NOT NULL );"); - // Create the .installed file in the server folder - file_put_contents(SERVER.'/.installed', 'Installed on '.date('Y-m-d H:i:s')); - echo render('install/layout', ['title' => 'Database Setup', 'step' => 'second', 'complete' => $complete, 'start' => $istart]); } @@ -300,9 +297,11 @@ class InstallModule // login the admin App::$auth->login($_POST['username'], $_POST['password']); + // Create the .installed file in the server folder + file_put_contents(SERVER.'/.installed', 'Installed on '.date('Y-m-d H:i:s')); + // Render the finished page! echo render('install/layout', ['title' => 'Finished!', 'step' => 'done', 'name' => $_POST['username'], 'complete' => $_POST['complete'] ?? false]); - } private static function fourOhFour() diff --git a/server/templates/gate/login.php b/server/templates/gate/login.php index ce01362..2b87bae 100644 --- a/server/templates/gate/login.php +++ b/server/templates/gate/login.php @@ -1 +1,7 @@ -hello +
+ + + + + +
diff --git a/server/templates/layout.php b/server/templates/layout.php index 5b39f34..8c9dde9 100644 --- a/server/templates/layout.php +++ b/server/templates/layout.php @@ -14,7 +14,9 @@
@@ -22,7 +24,9 @@
diff --git a/server/templates/partials/left.php b/server/templates/partials/left.php new file mode 100644 index 0000000..2404dce --- /dev/null +++ b/server/templates/partials/left.php @@ -0,0 +1 @@ +Left diff --git a/server/templates/partials/right.php b/server/templates/partials/right.php new file mode 100644 index 0000000..99e966f --- /dev/null +++ b/server/templates/partials/right.php @@ -0,0 +1 @@ +Right