Skip to content

Commit

Permalink
Merge pull request #9 from DasunNethsara-04/main
Browse files Browse the repository at this point in the history
JWT added
  • Loading branch information
DasunNethsara-04 authored Oct 28, 2024
2 parents 2fb5c04 + 23cbbb2 commit 41f3804
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ DB_HOST="<server IP>"
DB_USER="<username>"
DB_PASS="<password>"
DB_NAME="<db name>"

# JWT configs
JWT_SECRET="your-super-secret-key"
JWT_ISSUER="your-domain.com"
16 changes: 16 additions & 0 deletions App/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace ZenithPHP\App\Models;

use ZenithPHP\Core\Model\Model;

class User extends Model
{
protected string $table_name = 'users';

public static function verifyCredentials(string $username, string $password): int|bool
{
// TODO: Implement verifyCredentials() method.
return 1;
}
}
4 changes: 4 additions & 0 deletions Core/src/Http/InitEnv.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
13 changes: 13 additions & 0 deletions Core/src/Includes/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ZenithPHP\Core\Includes;

use Firebase\JWT\JWT;
use Random\RandomException;

class Security
Expand All @@ -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
*/
Expand Down
11 changes: 1 addition & 10 deletions Public/router.php
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
<?php
// DO NOT DELETE THIS FILE...
if (php_sapi_name() === 'cli-server') {
// Serve the requested resource as-is if it exists
$filePath = __DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if (file_exists($filePath) && !is_dir($filePath)) {
return false;
}
}
require 'index.php';
<?php if (php_sapi_name() === 'cli-server') { $filePath = __DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); if (file_exists($filePath) && !is_dir($filePath)) return false; } require 'index.php';
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"require": {
"vlucas/phpdotenv": "^5.6",
"ext-pdo": "*",
"ext-openssl": "*"
"ext-openssl": "*",
"firebase/php-jwt": "^6.10"
},
"autoload": {
"psr-4": {
Expand All @@ -23,4 +24,4 @@
"ZenithPHP\\Migrations\\": "Migrations/"
}
}
}
}
68 changes: 66 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 41f3804

Please sign in to comment.