please help,
I have a problem with my code, I tried to provide JWT Validation at login, replace Session Php, I managed to get a token when the user has sent an email and password which is validated with data in the database, but I have a problem after the user gets the token and will executes the next command, namely
:Error: “kid” empty, unable to lookup correct key.
I know maybe because I haven’t given an ID to every data that I have validated and retrieved from the database, but I’m confused and need your help, experience is a valuable value. Thank You.
<?php
require_once __DIR__.'/../vendor/autoload.php';
use FirebaseJWTJWT;
use FirebaseJWTKey;
class Session
{
public function __construct(public string $username, public string $role)
{
}
}
class SessionManager
{
public static string $SECRET_KEY = "ahwehahewaheksmdashdadjwadmawduaskahkk";
public static function login(string $username, string $password): bool
{
try {
$pdo = new PDO("mysql:host=localhost;dbname=assabil", "root", "");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$statement = $pdo->prepare("SELECT Username, password_col FROM pengurus");
$statement->execute();
$row = $statement->fetch(PDO::FETCH_ASSOC);
if ($row && password_verify($password, $row['password_col'])) {
$payload = [
'uid' => $id,
"Username" => $username,
"role" => "customer"
];
$jwt = JWT::encode($payload, SessionManager::$SECRET_KEY, 'HS256');
setcookie("A-SSB-SESSION", $jwt);
return true;
} else {
return false;
}
} catch (PDOException $e) {
return false;
}
}
public static function getCurrentSession(): Session
{
if (isset($_COOKIE['A-SSB-SESSION'])) {
$jwt = $_COOKIE['A-SSB-SESSION'];
try {
$payload = JWT::decode($jwt, SessionManager::$SECRET_KEY, ['HS256']);
var_dump($payload); // Check the value of $payload
return new Session($payload->Username, $payload->role);
} catch (Exception $exception) {
echo "Error: " . $exception->getMessage();
die(); // Terminate the script to see the error message
}
} else {
throw new Exception("User is not login");
}
}
}
?>
I’m trying to find a solution regarding JWT Token validation, I hope you can get a solution.