perfected automated logins
All checks were successful
Build and push / Pulling repo on server (push) Successful in 3s

This commit is contained in:
Chris 2023-10-29 19:52:08 +01:00
parent e9a718ee96
commit 0ed8ab9137
7 changed files with 39 additions and 33 deletions

View File

@ -7,6 +7,5 @@
"github.copilot-chat",
"anbuselvanrocky.bootstrap5-vscode",
"hansuxdev.bootstrap5-snippets",
"brapifra.phpserver"
]
}

5
.vscode/launch.json vendored
View File

@ -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",
},
}
]

View File

@ -138,3 +138,35 @@ function getMenu()
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('<meta http-equiv="Refresh" content="seconds; url='. $url.'"> <script>window.location.href="'. $url.'"</script> ');
}
else //invalid cookie gets deleted
{
setcookie('token', '', time() - 3600, "/");
}
}
}

View File

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

View File

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

View File

@ -4,7 +4,7 @@
<h1>Profil</h1>
<div class="row">
<div class="col-3">
<img src="<?= $user['photo']?:'https://pictshare.net/1ch3e5.png' ?>/300x170/fixedsize" class="card-img-top" alt="<?= escape($user['name']); ?>'s profile Picture">
<img src="<?= $user['photo']?:'https://pictshare.net/1ch3e5.png' ?>/300x170/forcesize" class="card-img-top" alt="<?= escape($user['name']); ?>'s profile Picture">
</div>
<div class="col-9">

View File

@ -5,7 +5,7 @@
<div class="row">
<div class="col-3">
<div class="card">
<img src="<?= $dogdata['photo']?:'https://pictshare.net/1ch3e5.png' ?>/300x170/fixedsize" class="card-img-top" alt="<?= escape($dogdata['name']); ?>'s profile Picture">
<img src="<?= $dogdata['photo']?:'https://pictshare.net/1ch3e5.png' ?>/300x170/forcesize" class="card-img-top" alt="<?= escape($dogdata['name']); ?>'s profile Picture">
<div class="card-body">
<h5 class="card-title"><?= escape($dogdata['name']); ?></h5>
<p class="card-text">