From b7a7b6ba8a54eefa2a150c83e382c605ecf8a404 Mon Sep 17 00:00:00 2001 From: samapraku Date: Tue, 13 Oct 2020 15:55:34 +0200 Subject: [PATCH] Redirect to login page if not logged in --- src/bb-di.php | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/bb-di.php b/src/bb-di.php index f6a74de07..c7a089e4d 100644 --- a/src/bb-di.php +++ b/src/bb-di.php @@ -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; };