I try to learn how to manage session with JWT. But now I’m stuck because i always get exception after login, and it always redirect me to login page again and again.
There is no error detected, and i can’t find what’s wrong in my code. So i hope you all can help me although it is just look like a simple questions
I try to browse to check where is the problem. And i’m sure the problem is in the file session.php. But i don’t know whats wrong
<?php
// use FirebaseJWTJWT;
class Session
{
public $username, $role;
public function __construct(string $username, string $role)
{
$this->username = $username;
$this->role = $role;
}
}
class SessionManager
{
// public $SECRET_KEY = 'ajfhakjdfhah/A203FHkafhiuefhhncueuvuwevwevwev';
public static function login(string $username, string $password): bool
{
if ($username == "eko" && $password == "eko") {
$SECRET_KEY = 'AKDJHFEVN123akdhfvbuevmkc';
$payload = [
"username" => $username,
"role" => "customer"
];
$jwt = FirebaseJWTJWT::encode($payload, $SECRET_KEY, 'HS256');
setcookie('USER-SESSION', $jwt);
return true;
} else {
return false;
}
}
public static function getCurrentSession(): Session
{
if ($_COOKIE['USER-SESSION']) {
$jwt = $_COOKIE['USER-SESSION'];
$SECRET_KEY = 'AKDJHFEVN123akdhfvbuevmkc';
$payload = FirebaseJWTJWT::decode($jwt, $SECRET_KEY, ['HS256']);
try {
$payload = FirebaseJWTJWT::decode($jwt, $SECRET_KEY, ['HS256']);
return new Session($payload->username, $payload->role);
} catch (Exception $exception) {
throw new Exception("User is not login");
}
} else {
throw new Exception("User is not login");
}
}
}