diff --git a/web/src/base.php b/web/src/base.php index 474c154..5cc5175 100644 --- a/web/src/base.php +++ b/web/src/base.php @@ -40,8 +40,30 @@ function tpl_styles($styles) { } -function cookie_redirect_app() { - // TODO: also check if cookie is valid to redirect - if (isset($_COOKIE['fedilove_session'])) +function 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); } diff --git a/web/src/public/login.php b/web/src/public/login.php index bf3828d..fb3e964 100644 --- a/web/src/public/login.php +++ b/web/src/public/login.php @@ -1,5 +1,5 @@ - + ' s(login)', 'scripts' => ['http']