-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from wanderlust-group-project-1/nirmal
Nirmal
- Loading branch information
Showing
9 changed files
with
213 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<?php | ||
use Firebase\JWT\JWT; | ||
use Firebase\JWT\Key; | ||
|
||
|
||
class AdminMiddleware { | ||
|
||
static $user = []; | ||
|
||
static $allowedColumns = ['id', 'email', 'name', 'role']; | ||
|
||
// filter user with allowed columns | ||
|
||
public static function getUser(): array { | ||
|
||
|
||
// return array_filter(Self::$user, function ($key) { | ||
// return in_array($key, Self::$allowedColumns); | ||
// }, ARRAY_FILTER_USE_KEY); | ||
|
||
// check if user is admin | ||
if (Self::$user['role'] == 'admin') { | ||
return true; | ||
} | ||
else{ | ||
return false; | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
|
||
// protected static $user; | ||
|
||
public static function run_middleware(string $controller, string $method): mixed { | ||
// show($controller); | ||
$authRequired = [ | ||
'Dashboard' => ['index', 'method2'], | ||
'Controller2' => ['method3'], | ||
'Customer' => ['index', 'edit', 'update'], | ||
'Profile' => ['index', 'edit', 'update'], | ||
// 'Profile' => ['index', 'edit', 'update'], | ||
]; | ||
$unauthRequired = [ | ||
'Login' => ['index'], | ||
'Signup' => ['index'] | ||
]; | ||
|
||
$currentController = ucfirst($controller); | ||
|
||
if (isset($authRequired[$currentController]) && | ||
in_array($method, $authRequired[$currentController])) { | ||
Self::is_authenticated(); | ||
} | ||
if (isset($unauthRequired[$currentController]) && | ||
in_array($method, $unauthRequired[$currentController])) { | ||
Self::not_authenticated(); | ||
}else { | ||
Self::check(); | ||
} | ||
|
||
// return Self::$user; | ||
// return Self::getUser(); | ||
return true; | ||
|
||
|
||
} | ||
|
||
private static function check():mixed { | ||
$cookieName = 'jwt_auth_token'; | ||
// print_r($_COOKIE); | ||
if (!isset($_COOKIE[$cookieName])) { | ||
setcookie('jwt_auth_token', '', time() - 1, '/'); | ||
// redirect('login'); | ||
return false; | ||
} | ||
|
||
$token = $_COOKIE[$cookieName]; | ||
|
||
try { | ||
// echo $token; | ||
$decoded = JWT::decode($token, new Key( SECRET_KEY, 'HS256')); | ||
// The token is valid; you can access the claims as $decoded->id, $decoded->email, | ||
|
||
$user = new UserModel; | ||
|
||
// show($user); | ||
// $data = [] | ||
// $userId = $decoded->user_id; | ||
$data['email'] = $decoded->email; | ||
// $email = $decoded->email; | ||
// show($user->first($data)); | ||
|
||
$userData = $user->first($data); | ||
if(!$userData){ | ||
setcookie('jwt_auth_token', '', time() - 1, '/'); | ||
// redirect('login'); | ||
return false; | ||
} | ||
// return $userData; | ||
// std class to array | ||
// $this->$user = (array) $userData; | ||
|
||
// check if user is admin | ||
// Self::$user = (array) $userData; | ||
|
||
if ($userData->role == 'admin') { | ||
Self::$user = (array) $userData; | ||
$_SESSION['ADMIN'] = (array) $userData; | ||
}else{ | ||
setcookie('jwt_auth_token', '', time() - 1, '/'); | ||
redirect('admin/login'); | ||
// return false; | ||
} | ||
|
||
|
||
|
||
|
||
// Authorization checks | ||
|
||
} catch (Exception $e) { | ||
// Token is invalid; return an error response | ||
// http_response_code(401); | ||
// echo json_encode(['error' => 'Token is invalid']); | ||
// exit(); | ||
setcookie('jwt_auth_token', '', time() - 1, '/'); | ||
// redirect('login'); | ||
return false; | ||
} | ||
return true; | ||
} | ||
public static function is_authenticated():void { | ||
if(!self::check()){ | ||
redirect('admin/login'); | ||
} | ||
} | ||
public static function not_authenticated():void { | ||
if(self::check()){ | ||
redirect('admin/dashboard'); | ||
} | ||
} | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters