Redirect to login page if not logged in

This commit is contained in:
samapraku committed 2020-10-13 15:55:34 +02:00
1 parent 0f7e238626
commit b7a7b6ba8a
1 file changed
+23 -3
+23 -3
View File
@@ -168,17 +168,37 @@ $di['twig'] = $di->factory(function () use ($di) {
$di['is_client_logged'] = function() use($di) {
if(!$di['auth']->isClientLoggedIn()) {
$url = $di['url']->link('login');
header("Location: $url");
throw new Exception('Client is not logged in');
$api_str = '/api/';
$url = $di['request']->getQuery('_url');
if (strncasecmp($url, $api_str , strlen($api_str)) === 0) {
//Throw Exception if api request
throw new Exception('Client is not logged in');
}
else {
//Redirect to login page if browser request
$login_url = $di['url']->link('login');
header("Location: $login_url");
}
}
return true;
};
$di['is_admin_logged'] = function() use($di) {
if(!$di['auth']->isAdminLoggedIn()) {
$api_str = '/api/';
$url = $di['request']->getQuery('_url');
if (strncasecmp($url, $api_str , strlen($api_str)) === 0) {
//Throw Exception if api request
throw new Exception('Admin is not logged in');
}
else {
//Redirect to login page if browser request
$login_url = $di['url']->adminLink('staff/login');
header("Location: $login_url");
}
}
return true;
};