How to decode JWT using JWT-auth in laravel

How to decode JWT using JWT-auth in laravel

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding JWT Payloads in Laravel Without JWT-auth

A common stumbling block when integrating JWT authentication into a Laravel backend is the realization that the dedicated middleware does not provide a direct method to decode tokens for arbitrary data extraction. Many developers encounter this specific scenario when building mobile applications, such as Android clients, where the server needs to read claims from a token without validating it against a session or user record first. The spatie/laravel-jwt-auth package (versions 0.5 and similar iterations) is designed primarily for stateless authentication middleware, not for utility functions to parse raw strings. To fetch information from the payload, you must bypass the authentication layer entirely and use the underlying library directly.

The standard approach involves using lcobucci/jwt, which is the foundational library powering most Laravel JWT packages. In Laravel 8, 9, and 10, this is often exposed via the Lcobucci\JWT\Parser class if you have the dependency installed, or more commonly through the spatie/laravel-jwt-auth helper classes even in older versions. However, relying on auth()->user() inside a middleware or controller to decode a token will fail because that method assumes the token has already passed validation and been attached to the request lifecycle.

To decode a raw JWT string without triggering authentication logic, you need to instantiate the parser manually. Here is a practical implementation using the standard library approach found in modern setups:

use Lcobucci\JWT;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\ValidationData;
use Lcobucci\OpenidConnect\Request as OpenidConnectRequest;

// This example assumes you have access to the public key or secret used to sign the token.
$config = Configuration::forSymmetricSigner(
    new \Lcobucci\Clocks\SystemClock(),
    new \Lcobucci\Calendar\InfiniteTimeLimit(),
    'your-secret-key' // Replace with your actual signing key
);

$parser = $config->parser();

// Decode the token string passed from the Android client
$tokenString = $request->header('Authorization') ?? $request->input('token');
$decodedPayload = $parser->parse($tokenString)->claims()->all();

// Access specific claims directly without validation
$userEmail = $decodedPayload['email'] ?? null;
$userId = $decodedPayload['sub'] ?? null;

If you are strictly using the older spatie/laravel-jwt-auth package (0.5 series), the interface is slightly different, but the principle remains identical: do not use the auth() helper. Instead, access the static methods provided by the package's configuration or instantiate the parser from the service provider. The package usually registers a Config class that holds the signer and clock instances required for parsing.

use Spatie\JWTPayloads\Config;
// Or depending on the specific 0.5.x version, you might need to access the underlying Lcobucci instance
use Lcobucci\JWT\Parser;

// Assuming you can inject the signer configuration or have the secret key
$parser = new Parser(); 
// Note: In strict implementations without validation, you must ensure the token format is correct.
// The parser will throw an exception if the signature does not match your configured signing method.

A critical trade-off to consider here is the separation of concerns between decoding and validating. Decoding a token simply reveals its contents; it does not verify that the token has not expired, nor does it confirm that the signature matches your server's secret key. If your application requires these checks before trusting the data, you must explicitly invoke the validation methods available on the parser or configuration object before accessing the payload. Skipping validation opens the door to replay attacks and injection of malicious claims by a client who can forge tokens if they possess your signing key.

When working with Android clients, ensure the token format matches exactly what your Laravel backend expects, including the Authorization: Bearer <token> header format versus query parameters. The payload structure is dictated entirely by your own claims definition; there is no universal standard for the email or sub fields beyond common convention. Always validate that the decoded data contains the necessary fields before proceeding with business logic to avoid runtime exceptions accessing null keys.

For developers needing a robust solution that handles both decoding and validation seamlessly, reviewing resources from https://laravelcompany.com regarding JWT implementation patterns can provide additional context on configuring the underlying signer objects correctly for different environments.

Our network