Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase JWT: Signature verification failed

I'm trying to use a JWT authentication with Firebase, but I got always this error: "Fatal error: Uncaught Firebase\JWT\SignatureInvalidException: Signature verification failed".

The code is this:

$key = "test";

$tokenId    = base64_encode(mcrypt_create_iv(32));
$issuedAt   = time();
$notBefore  = $issuedAt + 10;
$expire     = $notBefore + 60;
$serverName = $_SERVER["SERVER_NAME"];


$data = [
    'iat'  => $issuedAt,
    'jti'  => $tokenId,
    'iss'  => $serverName,
    'nbf'  => $notBefore,
    'exp'  => $expire,
    "userId"  => 1
];


$secretKey = base64_decode($key);


$jwt = \Firebase\JWT\JWT::encode($data, $secretKey, 'HS256');

// and when I decode the tokens, I got that exception
$decoded = \Firebase\JWT\JWT::decode($jwt, $key, array('HS256'));

What I wrong?

like image 747
Webman Avatar asked Oct 17 '25 01:10

Webman


1 Answers

You don't need $secretKey or base64_decode the key for that matter, just do:

$jwt = \Firebase\JWT\JWT::encode($data, $key, 'HS256');
$decoded = \Firebase\JWT\JWT::decode($jwt, $key, array('HS256'));
like image 124
Hans Z. Avatar answered Oct 19 '25 15:10

Hans Z.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!