diff --git a/app/controllers/Logout.php b/app/controllers/Logout.php index e7cd9930..46256ec6 100644 --- a/app/controllers/Logout.php +++ b/app/controllers/Logout.php @@ -6,6 +6,7 @@ class Logout { public function index(): void { $data['email'] = empty($_SESSION['USER']) ? '' : $_SESSION['USER']->email; + setcookie('jwt_auth_token', '', time() - 1, '/'); session_destroy(); redirect('home'); } diff --git a/app/core/App.php b/app/core/App.php index dca02778..cb012e73 100755 --- a/app/core/App.php +++ b/app/core/App.php @@ -116,9 +116,17 @@ public function loadController(): void { // } // } // $this->runMiddleware(); - $user = AuthMiddleware::run_middleware($this->controller, $this->method); + $user = UserMiddleware::user(AuthMiddleware::run_middleware($this->controller, $this->method)); + // make user global to all views + + $_SESSION['USER'] = $user; + // show($user); + // show($_SESSION['USER']); + + // show($user); call_user_func_array([$controller, $this->method], $URL); + } } diff --git a/app/core/init.php b/app/core/init.php index c07e7841..0c001cb4 100755 --- a/app/core/init.php +++ b/app/core/init.php @@ -22,3 +22,4 @@ require 'Controller.php'; require 'App.php'; require '../app/middlewares/AuthMiddleware.php'; +require '../app/middlewares/UserMiddleware.php'; diff --git a/app/middlewares/AuthMiddleware.php b/app/middlewares/AuthMiddleware.php index 795dfd85..fdc2ce64 100644 --- a/app/middlewares/AuthMiddleware.php +++ b/app/middlewares/AuthMiddleware.php @@ -25,6 +25,7 @@ public static function run_middleware(string $controller, string $method): mixed $authRequired = [ 'Home' => ['index', 'method2'], 'Controller2' => ['method3'], + // 'Profile' => ['index', 'edit', 'update'], ]; $unauthRequired = [ 'Login' => ['index'], @@ -40,6 +41,8 @@ public static function run_middleware(string $controller, string $method): mixed if (isset($unauthRequired[$currentController]) && in_array($method, $unauthRequired[$currentController])) { Self::not_authenticated(); + }else { + Self::check(); } // return Self::$user; diff --git a/app/middlewares/UserMiddleware.php b/app/middlewares/UserMiddleware.php index e69de29b..6940fd97 100644 --- a/app/middlewares/UserMiddleware.php +++ b/app/middlewares/UserMiddleware.php @@ -0,0 +1,47 @@ +first([ + 'user_id'=> $user['id'] + ]); + // echo $customer; + $user = (object) array_merge((array) $user, (array) $customer); + return $user; + } + if ($user['role'] == 'guide') { + $guide = new GuideModel; + $guide = $guide->first([ + 'user_id'=> $user['id'] + ]); + // show($d); + $user = (object) array_merge((array) $user, (array) $guide); + return $user; + } + if ($user['role'] == 'rental_service') { + $rental_service = new RentalServiceModel; + $rental_service = $rental_service->first([ + 'user_id'=> $user['id'] + ]); + $user = (object) array_merge((array) $user, (array) $rental_service); + return $user; + } + } + +} + + + + + + + +?> \ No newline at end of file diff --git a/app/models/Customer.php b/app/models/Customer.php index 978de850..753f360a 100644 --- a/app/models/Customer.php +++ b/app/models/Customer.php @@ -9,6 +9,7 @@ class CustomerModel { 'address', 'number', 'nic', + 'user_id' ]; public function registerCustomer(array $data){ diff --git a/app/views/admin/components/navbar.php b/app/views/admin/components/navbar.php index 4d047e7e..f8d0f461 100644 --- a/app/views/admin/components/navbar.php +++ b/app/views/admin/components/navbar.php @@ -11,5 +11,7 @@