Example code for a sample Project...of PHP Code Hypdrater
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

47 lines
1.5 KiB

<?php
declare(strict_types = 1);
/**
* @author Robert Strutts <Bob_586@Yahoo.com>
* @copyright (c) 2025, Robert Strutts
* @license MIT
*/
namespace Project\controllers\app;
use CodeHydrater\bootstrap\registry as Reg;
class jwt_ctrl {
public function index() {
// Payload data (customize as needed)
$payload = [
'iss' => 'your-issuer', // Issuer
'aud' => 'your-audience', // Audience
'iat' => time(), // Issued at (timestamp)
'exp' => time() + 3600, // Expiration time (1 hour)
'sub' => 'user123', // Subject (user ID)
'role' => 'admin' // Custom claim (e.g., user role)
];
try {
$access_token = Reg::get('di')->get_service('JWT_Encode', $payload);
echo "Access Token: ". $access_token . "<br>\n";
echo "<a href=\"". \CodeHydrater\misc::get_url("app/jwt", "read", ['token' => $access_token] ) ."\">Test Token</a>";
} catch (\Exception $e) {
echo "Error generating token: " . $e->getMessage();
}
}
public function read(array $p) {
$token = $p['token'] ?? false;
if ($token === false) {
echo "Sorry, no token in URL";
exit(1);
}
try {
\CodeHydrater\common::dump(Reg::get('di')->get_service('JWT_Decode', $token));
} catch (\Exception $e) {
echo "Invalid token: " . $e->getMessage();
}
}
}