From 0ed8ab91371de1aadb4657ab6452bd6830cdd5f8 Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 29 Oct 2023 19:52:08 +0100 Subject: [PATCH] perfected automated logins --- .vscode/extensions.json | 1 - .vscode/launch.json | 5 ++-- web/inc/core.php | 32 ++++++++++++++++++++++++++ web/index.php | 2 ++ web/pages/err/controller.php | 28 ---------------------- web/pages/profile/profile.html | 2 +- web/pages/tournaments/tournaments.html | 2 +- 7 files changed, 39 insertions(+), 33 deletions(-) diff --git a/.vscode/extensions.json b/.vscode/extensions.json index a449737..f48553b 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -7,6 +7,5 @@ "github.copilot-chat", "anbuselvanrocky.bootstrap5-vscode", "hansuxdev.bootstrap5-snippets", - "brapifra.phpserver" ] } \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index 5b0fbbc..08db8a2 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,10 +11,11 @@ "-t", "web" ], - "hostname": "localhost", "port": 5902, "serverReadyAction": { - "action": "openExternally" + "action": "openExternally", + "pattern": "listening on http://localhost:([0-9]+)", + "uriFormat": "http://localhost:%s", }, } ] diff --git a/web/inc/core.php b/web/inc/core.php index dbc8617..7ed9d80 100644 --- a/web/inc/core.php +++ b/web/inc/core.php @@ -137,4 +137,36 @@ function getMenu() $arr = array_values($arr); return $arr; +} + +function autoLoginCheck() +{ + //check if user has a cookie and if so, logg them in and refresh the page + if(isset($_COOKIE['token']) && $_COOKIE['token'] != '' && !$_SESSION['user']) + { + $u = new User(); + $allusers = $u->getAll(false); + foreach($allusers as $user) + { + if($user['token'] && $user['token'] == $_COOKIE['token']) + { + $u->id = $user['id']; + break; + } + } + + if($u->id) //valid cookie, users gets logged in + { + $u->load($u->id); + $u->login(); + $url = '/'.implode('/',$GLOBALS['url']); + header("HX-Redirect: ". $url); + exit(' '); + } + else //invalid cookie gets deleted + { + + setcookie('token', '', time() - 3600, "/"); + } + } } \ No newline at end of file diff --git a/web/index.php b/web/index.php index 83a59da..b303a84 100644 --- a/web/index.php +++ b/web/index.php @@ -29,6 +29,8 @@ if($url==[] && $_SERVER['HTTP_HX_CURRENT_URL']) $GLOBALS['url'] = $url; //echo print_r(['url'=>$url,'server'=>$_SERVER,'request'=>$_REQUEST,'cookie'=>$_COOKIE,'session'=>$_SESSION],true); +autoLoginCheck(); + $response = callHook($url); if(is_string($response)) diff --git a/web/pages/err/controller.php b/web/pages/err/controller.php index 9c3ed8f..07a3646 100644 --- a/web/pages/err/controller.php +++ b/web/pages/err/controller.php @@ -11,34 +11,6 @@ class Err extends Page { function notallowed() { - //check if user has a cookie and if so, logg them in and refresh the page - if(isset($_COOKIE['token'])) - { - $u = new User(); - $allusers = $u->getAll(false); - foreach($allusers as $user) - { - if($user['token'] && $user['token'] == $_COOKIE['token']) - { - $u->id = $user['id']; - break; - } - } - - if($u->id) - { - $u->load($u->id); - $u->login(); - $url = '/'.implode('/',$GLOBALS['url']); - //var_dump($url); - $this->redirect($url); - } - else - { - setcookie('token', '', time() - 3600, "/"); - } - } - $this->set("loggedin",(isset($_SESSION['user']) && $_SESSION['user'] !== false)); $this->set('template', "notallowed.html"); } diff --git a/web/pages/profile/profile.html b/web/pages/profile/profile.html index 4716d06..224804f 100644 --- a/web/pages/profile/profile.html +++ b/web/pages/profile/profile.html @@ -4,7 +4,7 @@

Profil

- <?= escape($user['name']); ?>'s profile Picture + <?= escape($user['name']); ?>'s profile Picture
diff --git a/web/pages/tournaments/tournaments.html b/web/pages/tournaments/tournaments.html index b4d0958..0ca4598 100644 --- a/web/pages/tournaments/tournaments.html +++ b/web/pages/tournaments/tournaments.html @@ -5,7 +5,7 @@
- <?= escape($dogdata['name']); ?>'s profile Picture + <?= escape($dogdata['name']); ?>'s profile Picture