working on login

This commit is contained in:
Sky Johnson 2024-07-17 18:41:34 -05:00
parent d8446b7d3c
commit a20c443178
10 changed files with 43 additions and 14 deletions

View File

@ -19,3 +19,6 @@ if ($route == 'gate') return GateModule::handle();
// 404
http_response_code(404);
echo '404: ' . $route;
// cleanup
$app->cleanup();

View File

@ -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']);
}
}

View File

@ -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);

View File

@ -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('/');
}
}

View File

@ -4,8 +4,13 @@ class HomeModule
{
public static function home()
{
foreach ($_SESSION['flash'] as $key => $value) {
echo '<div class="alert ' . $key . '">- ' . $value . '</div>';
}
if (App::auth()) {
echo 'You are already logged in!<br>';
echo '<form action="/gate/logout" method="post"><button>Logout</button></form>';
} else {
echo 'You are not logged in!<br>';
}

View File

@ -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()

View File

@ -1 +1,7 @@
hello
<form action="/gate/login" method="post">
<input type="text" name="id" placeholder="Email or Username">
<input type="password" name="pw" placeholder="Password">
<input type="checkbox" name="remember" id="remember">
<label for="remember">Remember me</label>
<button>Login</button>
</form>

View File

@ -14,7 +14,9 @@
<div id="content">
<aside id="left">
left
<?php if (App::$auth->good()) {
echo render('partials/left', $data);
} ?>
</aside>
<main>
@ -22,7 +24,9 @@
</main>
<aside id="right">
right
<?php if (App::$auth->good()) {
echo render('partials/right', $data);
} ?>
</aside>
</div>

View File

@ -0,0 +1 @@
Left

View File

@ -0,0 +1 @@
Right