Set expiry time for laravel jwt dynamically

Set expiry time for laravel jwt dynamically

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

Stefan Bogdanescu

Founder & Senior Architect

Set Expiry Time for Laravel JWT Dynamically

When implementing "Remember Me" functionality in a web application, you immediately face a configuration dilemma. The standard config/jwt.php file provided by Tymon's JWT library offers a global ttl (time-to-live) setting, typically defaulting to 60 minutes. This approach works fine for session-based authentication where security and token rotation are paramount. However, forcing every user into this same 1-hour window makes the "Remember Me" checkbox feel ineffective.

To support different expiry durations based on user intent—like granting a week of access versus a standard hour—you need to move away from a static global configuration. The solution involves decoupling the token expiration logic from the constant file and injecting it dynamically during the authentication process.

Leveraging Custom Claims for Expiration

The most robust pattern for this scenario is to store the desired expiration time as a custom claim within the JWT payload itself. Since JWTs are symmetric, you have full access to modify the claims array in your AuthController before signing the token. Instead of relying solely on the global ttl variable in config/jwt.php, you can calculate a specific duration based on the input state and pass it directly to the generator.

In your login request handler, check if the user has selected the "Remember Me" option. If they have, convert that intent into minutes (e.g., 7 days equals 10,080 minutes) and include this value in the claims array. The Tymon JWT library allows you to pass a custom duration argument or store a string claim if your version of the library supports dynamic TTL arguments directly in the factory() method.

If you are using the standard Auth::createToken flow, you might need to adjust how you construct the token. You can override the default behavior by passing an array of claims that includes a custom key representing the expiration offset:

use Tymon\JWTAuth\Facades\JWTFactory;

// Detect 'remember' intent
$ttl = request()->has('remember') ? 10080 : 60; // 7 days vs default hour

// Generate token with dynamic claims
$token = JWTFactory::createToken($user);

// If using a custom claim to store the intended expiry logic
$token->getPayload()->set('remember_ttl', $ttl);

Note that simply adding a custom key to the payload does not automatically alter the exp claim unless the library or your custom factory logic reads that specific key. With Tymon's latest iterations, you often need to ensure the exp claim is calculated based on this dynamic value rather than the config constant. You may need to manually set the expiration time using Carbon:

use Illuminate\Support\Carbon;

$rememberTtl = request()->has('remember') ? 10080 : 60;
$expirationTime = Carbon::now()->addMinutes($rememberTtl);

$token->getPayload()->set('exp', $expirationTime->getTimestamp());

Security Trade-offs and Validation

Introducing dynamic TTLs brings a significant responsibility: validation. When you grant a token valid for a week, you are trusting the client's assertion that this is what they want. You must ensure that your backend logic consistently applies these rules without relying on the frontend to send the correct duration every time.

The primary risk here is session fixation or replay attacks if the token remains valid indefinitely or for too long on untrusted networks. While "Remember Me" implies a convenience trade-off, you should still consider issuing a refresh token alongside the access token. The short-lived access token (even with 1 hour default) can be used to request a new one from a secure storage server using the refresh token, keeping the high-value "remember me" tokens in a more controlled environment if your architecture supports it.

Another pitfall involves cache invalidation. If you are caching user sessions or permissions, ensure that the logic handling token validation respects the exp claim strictly. The Tymon library handles this automatically, but custom middleware might override these checks if not carefully configured. Always verify that any custom middleware does not hardcode a check against the global config ttl.

Finally, remember to document this behavior for your frontend team (such as the Angular app using Satellizer). They need to know that the token expiration is client-side independent and should not be manually refreshed or regenerated based on local assumptions. Let the server dictate the validity window through the exp claim to maintain a secure handshake.

Our network