1. Go to this page and download the library: Download nextphp/jwt library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nextphp / jwt example snippets
// without composer: quire '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' => '[email protected]',
'role' => 'admin',
'iat' => time(),
'exp' => time() + 3600 // 1 hour expiration
];
// Generate a JWT
$token = $jwt->generate($payload);
echo "Generated Token: " . $token . "\n";
// 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";
}