ci-secure-api
ci-secure-api copied to clipboard
JWT issue
I need to replace jwt_helper.php with this:
use App\Models\UserModel;
use Config\Services;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
function getJWTFromRequest($authenticationHeader): string { if (is_null($authenticationHeader)) { //JWT is absent throw new Exception('Missing or invalid JWT in request'); } //JWT is sent from client in the format Bearer XXXXXXXXX return explode(' ', $authenticationHeader)[1]; }
function validateJWTFromRequest(string $encodedToken)
{
$key = Services::getSecretKey();
$decodedToken = JWT::decode($encodedToken, new Key($key, 'HS256'));
$userModel = new UserModel();
$userModel->findUserByEmailAddress($decodedToken->email);
}
function getSignedJWTForUser(string $email) { $issuedAtTime = time(); $tokenTimeToLive = getenv('JWT_TIME_TO_LIVE'); $tokenExpiration = $issuedAtTime + $tokenTimeToLive; $payload = [ 'email' => $email, 'iat' => $issuedAtTime, 'exp' => $tokenExpiration, ];
$jwt = JWT::encode($payload, Services::getSecretKey(), 'HS256');
return $jwt;
}
Thanks, this was a helpful post - I ran into the same issue