From e08272efac81efbbb30211b03775e4a0c7405e04 Mon Sep 17 00:00:00 2001 From: Dasun Date: Mon, 28 Oct 2024 12:38:58 +0530 Subject: [PATCH 1/4] .env updated for jwt | jwt added --- .env | 4 +++ .env.example | 4 +++ Public/router.php | 11 +------- composer.json | 5 ++-- composer.lock | 68 +++++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 78 insertions(+), 14 deletions(-) diff --git a/.env b/.env index f36f481..23fc852 100644 --- a/.env +++ b/.env @@ -9,3 +9,7 @@ DB_HOST="localhost" DB_USER="root" DB_PASS="" DB_NAME="mvc" + +# JWT configs +JWT_SECRET="your-super-secret-key" +JWT_ISSUER="your-domain.com" \ No newline at end of file diff --git a/.env.example b/.env.example index 1f7d09c..78b56d9 100644 --- a/.env.example +++ b/.env.example @@ -9,3 +9,7 @@ DB_HOST="" DB_USER="" DB_PASS="" DB_NAME="" + +# JWT configs +JWT_SECRET="your-super-secret-key" +JWT_ISSUER="your-domain.com" diff --git a/Public/router.php b/Public/router.php index 916a471..c7fa73a 100644 --- a/Public/router.php +++ b/Public/router.php @@ -1,10 +1 @@ - Date: Mon, 28 Oct 2024 13:07:17 +0530 Subject: [PATCH 2/4] new envs load --- Core/src/Http/InitEnv.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Core/src/Http/InitEnv.php b/Core/src/Http/InitEnv.php index 499df6e..307ac9e 100644 --- a/Core/src/Http/InitEnv.php +++ b/Core/src/Http/InitEnv.php @@ -24,5 +24,9 @@ public static function load(): void define('APP_NAME', $_ENV['APP_NAME']); define('APP_URL', $_ENV['APP_URL']); define('APP_VERSION', $_ENV['APP_VERSION']); + + // JWT info using $_ENV + define('JWT_SECRET', $_ENV['JWT_SECRET']); + define('JWT_ISSUER', $_ENV['JWT_ISSUER']); } } From ca969ff09d1e9bb3070cba652b85c38e3a5eeb3e Mon Sep 17 00:00:00 2001 From: Dasun Date: Mon, 28 Oct 2024 13:07:57 +0530 Subject: [PATCH 3/4] JWT token generator implemented --- Core/src/Includes/Security.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Core/src/Includes/Security.php b/Core/src/Includes/Security.php index 0271ce4..2bd61c9 100644 --- a/Core/src/Includes/Security.php +++ b/Core/src/Includes/Security.php @@ -2,6 +2,7 @@ namespace ZenithPHP\Core\Includes; +use Firebase\JWT\JWT; use Random\RandomException; class Security @@ -16,6 +17,18 @@ public static function verify_password(string $password, string $hash): bool return password_verify($password, $hash); } + public static function generateJWTToken(string|int $userId, string $issuer, string $secretKey, int $expiry = 3600): string + { + $payload = [ + 'iss' => $issuer, + 'sub' => $userId, + 'iat' => time(), + 'exp' => time() + $expiry, + ]; + + return JWT::encode($payload, $secretKey, 'HS256'); + } + /** * @throws RandomException */ From 23cbbb28eb37085f1c6c022b1d60c11532f8c7bb Mon Sep 17 00:00:00 2001 From: Dasun Date: Mon, 28 Oct 2024 13:08:16 +0530 Subject: [PATCH 4/4] default model user added --- App/Models/User.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 App/Models/User.php diff --git a/App/Models/User.php b/App/Models/User.php new file mode 100644 index 0000000..c4f5bdd --- /dev/null +++ b/App/Models/User.php @@ -0,0 +1,16 @@ +