nextphp/jwt

NextPHP/JWT 是一个简单的 PHP 库,用于编码和解码 JSON Web Tokens (JWT)。这个库是 NextPHP 框架的一部分,由 Vedat Yıldırım 开发。它提供了生成、验证和解码 JWT 令牌的方法,符合当前规范。

1.0.2 2024-06-27 05:42 UTC

This package is not auto-updated.

Last update: 2024-09-20 15:09:21 UTC


README

NGJWT 是一个简单的 PHP 库,用于编码和解码 JSON Web Tokens (JWT)。这个库是 LeanPHP 框架的一部分,由 Vedat Yıldırım 开发。它提供了生成、验证和解码 JWT 令牌的方法,符合当前规范。

安装

您可以通过 Composer 安装此包: https://packagist.org.cn/packages/leanphpio/jwt

composer install leanphpio/jwt

使用

生成 JWT

// without composer: require 'NGJWT.php';

// with composer
require 'vendor/autoload.php';

use LeanPHP\JWT\NGJWT;

// Initialize the JWT handler with a secret key
$secret = 'your_secret_key';
$jwt = new NGJWT($secret);

// Example payload
$payload = [
    'user_id' => 1,
    'username' => 'john.doe',
    'email' => 'john.doe@example.com',
    'role' => 'admin',
    'iat' => time(),
    'exp' => time() + 3600 // 1 hour expiration
];

// Generate a JWT
$token = $jwt->generate($payload);
echo "Generated Token: " . $token . "\n";

验证 JWT

// Example headers containing the JWT
$headers = [
    'Authorization' => 'Bearer ' . $token
];

// Authenticate the user using the token from the headers
try {
    if ($jwt->authenticate($headers)) {
        $user = NGJWT::getUser();
        echo "Authenticated User: \n";
        print_r($user);
    }
} catch (\Exception $e) {
    echo "Authentication failed: " . $e->getMessage() . "\n";
}

解码 JWT

// Decode the token directly
try {
    $decoded = $jwt->decode($token);
    echo "Decoded Token: \n";
    print_r($decoded);
} catch (\Exception $e) {
    echo "Decoding failed: " . $e->getMessage() . "\n";
}

示例

以下是一个更详细的示例,演示了如何生成、存储、验证和解码 JWT。

require 'vendor/autoload.php';

use leanphp\jwt\NGJWT;

// Initialize the JWT handler with a secret key
$secret = 'your_secret_key';
$jwt = new NGJWT($secret);

// Example payload
$payload = [
    'user_id' => 1,
    'username' => 'john.doe',
    'email' => 'john.doe@example.com',
    'role' => 'admin',
    'iat' => time(),
    'exp' => time() + 3600 // 1 hour expiration
];

// Generate a JWT
$token = $jwt->generate($payload);
echo "Generated Token: " . $token . "\n\n";

// Simulate storing the token in a database
echo "Storing the token in the database...\n\n";
// Example of database storage logic (not implemented here)
// storeTokenInDatabase($user_id, $token);

// Example headers containing the JWT
$headers = [
    'Authorization' => 'Bearer ' . $token
];

// Authenticate the user using the token from the headers
echo "Authenticating User...\n";
try {
    if ($jwt->authenticate($headers)) {
        $user = NGJWT::getUser();
        echo "Authenticated User: \n";
        print_r($user);
        echo "\n";
    }
} catch (\Exception $e) {
    echo "Authentication failed: " . $e->getMessage() . "\n";
}

// Decode the token directly
echo "Decoding the Token...\n";
try {
    $decoded = $jwt->decode($token);
    echo "Decoded Token: \n";
    print_r($decoded);
    echo "\n";
} catch (\Exception $e) {
    echo "Decoding failed: " . $e->getMessage() . "\n";
}

// Example output separation
echo "\n--------------------------\n";
echo "Example 1: Token Generation\n";
echo "Generated Token: " . $token . "\n";

echo "\n--------------------------\n";
echo "Example 2: Token Authentication\n";
try {
    if ($jwt->authenticate($headers)) {
        $user = NGJWT::getUser();
        echo "Authenticated User: \n";
        print_r($user);
        echo "\n";
    }
} catch (\Exception $e) {
    echo "Authentication failed: " . $e->getMessage() . "\n";
}

echo "\n--------------------------\n";
echo "Example 3: Token Decoding\n";
try {
    $decoded = $jwt->decode($token);
    echo "Decoded Token: \n";
    print_r($decoded);
    echo "\n";
} catch (\Exception $e) {
    echo "Decoding failed: " . $e->getMessage() . "\n";
}

许可证 此项目遵循 MIT 许可协议。有关更多详细信息,请参阅 LICENSE 文件。

致谢 此库是 LeanPHP 框架的一部分,由 Vedat Yıldırım 开发。

贡献 如果您发现任何问题或有改进建议,请随时在 GitHub 上提交问题或拉取请求。

README.md 文件提供了 NGJWT 库的概述,包括安装说明、使用示例和每个方法的详细解释。它还包括一个完整的示例,演示了如何生成、验证和解码 JWT 令牌。存储库名称 php-jwt 简洁且易于 SEO,使用户更容易在搜索 PHP 中的 JWT 解决方案时找到您的项目。