'keyOrKeyArray must be an instance of Firebase\\JWT\\Key key or an array of Firebase\\JWT\\Key keys", in php ci4
{ "title": "UnexpectedValueException", "type": "UnexpectedValueException", "code": 500, "message": "$keyOrKeyArray must be an instance of Firebase\JWT\Key key or an array of Firebase\JWT\Key keys", "file": "E:\var\www\html\project\spans\admin\vendor\firebase\php-jwt\src\JWT.php", "line": 416,
Solution 1:[1]
You can use the below example code -
<?php
namespace App\Controllers;
use App\Controllers\BaseController;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
class User extends BaseController
{
public function exampleMethod()
{
$issuedAt = time();
$expirationTime = $issuedAt + 60; // jwt valid for 60 seconds from the issued time
$payload = array( // Any random data
'userid' => 'Test_UID',
'name' => 'Sankhnad Mishra',
'iat' => $issuedAt,
'exp' => $expirationTime
);
$key = 'A_JWT_SECRET'; // Any string
$alg = 'HS256'; // This is alg
$token = JWT::encode($payload, $key, $alg); // Encode payload as a JWT Token
$decodedToken = JWT::decode($token, new Key($key, 'HS256')); // Decode token to a payload
$response = [
'token' => $token,
'decodedToken' => $decodedToken
];
print_r($response);
}
}
If you run above then you will get the below response -
{
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyaWQiOiJUZXN0X1VJRCIsIm5hbWUiOiJTYW5raG5hZCBNaXNocmEiLCJpYXQiOjE2NDU4MTU0OTUsImV4cCI6MTY0NTgxNTU1NX0.0CwT9quW8-teyob3ObRU5KQBfQYWamCSTVCrAk9UX-o",
"decodedToken": {
"userid": "Test_UID",
"name": "Sankhnad Mishra",
"iat": 1645815495,
"exp": 1645815555
}
}
So, just pick those pieces of code from above based on your requirement.
Solution 2:[2]
you make use of namespace
Use Firebase\JWT\Key;
// For encode:
$jwt = JWT: encoding ($payload, $key, 'HS256');
// For decoding:
$decodedToken = JWT:: decode ($jwt, new key ($key,'HS256'));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sankhnad Mishra |
| Solution 2 | hasan05 |
