[Fixed] Uncaught Error: Firebase\JWT\JWT::decode(): Argument #3 ($headers) cannot be passed by reference – Jwt

by
Ali Hasan
composer-php jwt

Quick Fix: Update your code to use the new API syntax and provide a stdClass instance for the $headers parameter as follows:

$decoded = JWT::decode($jwt, new Key($key, 'HS256'), $headers = new stdClass());

The Problem:

After integrating the Firebase JWT library in PHP code, an error is encountered: "Uncaught Error: Firebase\JWT\JWT::decode(): Argument #3 ($headers) cannot be passed by reference". The issue arises specifically when attempting to decode the JWT token. Despite trying a suggested solution of changing the argument from ['HS512'] to array(HS512'), the error persists.

The Solutions:

Solution 1: Using Updated API Syntax

The provided JWT library from Firebase has updated its API syntax. The correct code to decode a JWT is as follows:

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

$decoded = JWT::decode($jwt, new Key($key, 'HS256'));

This syntax utilizes a Key object to specify the secret key and algorithm used for decoding.

Alternatively, you can also pass the headers parameter explicitly:

$decoded = JWT::decode($jwt, new Key($key, 'HS256'), $headers = new stdClass());

Solution 2: Downgrade to the previous version.

If encountering the “Argument #3 ($headers) cannot be passed by reference” error while decoding JWT tokens using the Firebase JWT library, consider downgrading to the previous version. Firebase has made a change in header handling in version 6.6.0, released on June 13, 2023. Downgrading to version 6.5.0 or earlier should resolve the issue.

Q&A

How to fix Argument #3 ($headers) cannot be passed by reference error in PHP-JWT

Downgrade php-jwt library to version 6.5.0 using composer req firebase/php-jwt:6.5.0.

How to fix Argument #3 ($headers) cannot be passed by reference error in PHP-JWT

Update the code to use the new API with a Key object and an optional headers parameter, as shown in the README of the project.

Video Explanation:

The following video, titled "How to fix Unexpected Token in JSON error (for web developers ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Not a programmer? Read this! ** - If you get this error in an app you didn't make, and/or you're not a web developer... this video will not ...