From 6596a76c856e2ddfccbc5f03d12a524fdf80e1bf Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 26 Jan 2025 16:10:15 +0200 Subject: feat: Logout button --- controllers/login.php | 8 ++++++++ models/cookies.php | 7 +++++++ models/database.php | 6 ++++++ views/global/header.php | 6 ++++-- views/global/router.php | 1 + views/login/index.php | 31 ++++++++++++++++--------------- views/logout/index.php | 23 +++++++++++++++++++++++ views/logout/meta.php | 4 ++++ 8 files changed, 69 insertions(+), 17 deletions(-) create mode 100644 views/logout/index.php create mode 100644 views/logout/meta.php diff --git a/controllers/login.php b/controllers/login.php index 7502b03..8b640ce 100644 --- a/controllers/login.php +++ b/controllers/login.php @@ -21,3 +21,11 @@ function on_post() { $user_status = "User \"" . $_POST["username"] . "\" doesn't exist!"; } } + +function on_delete() { + try { + $headers = apache_request_headers(); + Database\Cookie::delete($headers["Authorization"]); + } + catch(Exception $e) {} +} diff --git a/models/cookies.php b/models/cookies.php index a181022..ddc7d1c 100644 --- a/models/cookies.php +++ b/models/cookies.php @@ -24,6 +24,13 @@ class Cookie extends Table { )->UID; return User::fromDBuid($UID); } + + static function delete(string $token) { + Table::_delete( + 'Cookies', + "Token = \"$token\"" + ); + } } // Taken from https://stackoverflow.com/a/15875555 diff --git a/models/database.php b/models/database.php index f472dbb..dc177e8 100644 --- a/models/database.php +++ b/models/database.php @@ -33,6 +33,12 @@ abstract class Table { return $id; } + static protected function _delete(string $table, string $condition) { + $conn = Table::connect(); + $query = $conn->query("DELETE FROM $table WHERE $condition"); + $conn = null; + } + static protected function _get_entries_count(string $table) : int { $conn = Table::connect(); $query = $conn->query("SELECT count(*) FROM $table"); diff --git a/views/global/header.php b/views/global/header.php index a00c528..863ba10 100644 --- a/views/global/header.php +++ b/views/global/header.php @@ -17,7 +17,8 @@
Login Register - + +
diff --git a/views/global/router.php b/views/global/router.php index 8986dfe..d77fcda 100644 --- a/views/global/router.php +++ b/views/global/router.php @@ -16,6 +16,7 @@ function route_view() { case '/profile': return '/profile'; case '/register': return '/register'; case '/login': return '/login'; + case '/logout': return '/logout'; } switch ($uri) { diff --git a/views/login/index.php b/views/login/index.php index 1013874..94faafe 100644 --- a/views/login/index.php +++ b/views/login/index.php @@ -1,20 +1,21 @@ - - -

- Fail: -

- -

- Success! -

- +

Login

+ +
+ +
+ + +

+ Error: +

+ + + - - diff --git a/views/logout/index.php b/views/logout/index.php new file mode 100644 index 0000000..fc9af5a --- /dev/null +++ b/views/logout/index.php @@ -0,0 +1,23 @@ +

Logging you out...

+ + diff --git a/views/logout/meta.php b/views/logout/meta.php new file mode 100644 index 0000000..be189c2 --- /dev/null +++ b/views/logout/meta.php @@ -0,0 +1,4 @@ +