Added improved session enforcing and redirection methods

This commit is contained in:
Niko 2022-02-10 02:08:46 +01:00
parent f9540b9aab
commit a3c0ac7ab6
2 changed files with 26 additions and 4 deletions

View File

@ -40,8 +40,30 @@ function tpl_styles($styles) {
} }
function cookie_redirect_app() { function session() {
// TODO: also check if cookie is valid to redirect if (!isset($_COOKIE['fedilove_session']))
if (isset($_COOKIE['fedilove_session'])) return null;
require_once 'database.php';
$session = null;
if (isset($GLOBALS['db_session']))
$session = $GLOBALS['db_session'];
else {
$session = (new DB())->findOne('u__sessions',
['session' => $_COOKIE['fedilove_session']]);
$GLOBALS['db_session'] = $session;
}
if ($session !== null && strlen((string)$session->_id) > 0)
return $session;
return null;
}
function session_enforce($redirect = '/') {
if (session() === null)
redirect($redirect);
}
function if_session_redirect_app() {
if (session() !== null)
redirect(APP_DIR); redirect(APP_DIR);
} }

View File

@ -1,5 +1,5 @@
<?php require 'base.php' ?> <?php require 'base.php' ?>
<?php cookie_redirect_app() ?> <?php if_session_redirect_app() ?>
<?php tpl('pub.header', [ <?php tpl('pub.header', [
'title' => ' s(login)', 'title' => ' s(login)',
'scripts' => ['http'] 'scripts' => ['http']