A user reports they were logged out in the middle of the day. Another says their mobile app has been signed in for six months straight. Both complaints can come from the same Laravel Sanctum setup, and both usually trace back to one misunderstanding: how Sanctum token expiry actually works. Here is the part most people miss, plus the pitfalls that follow from it.
Sanctum tokens do not expire by default
When you issue a personal access token with createToken(), Sanctum stores it in the personal_access_tokens table with no expiry date. The expiration option in config/sanctum.php defaults to null, and null means tokens never expire. If you never set it, every token you issue is valid forever, or until you revoke it. That is fine for a server-to-server integration you control. It is a problem for a mobile app token issued to a phone that gets lost, sold, or stolen.
Set an explicit lifetime:
// config/sanctum.php
'expiration' => 43200, // minutes: 30 days
Pick a lifetime that matches the client. A mobile app usually needs weeks, paired with a clean re-authentication flow, because forcing a daily login will push users toward workarounds.
Changing the lifetime affects existing tokens too
This is the pitfall that causes the surprise midday logouts. Sanctum checks expiry at authentication time by comparing the token's created_at against the configured lifetime. It does not stamp an expiry date onto the token when it is created. So the moment you deploy a 7-day lifetime, every token older than 7 days stops working at once, including tokens for users who have been happily signed in for months.
That is not necessarily wrong, but it is a deployment event, not a quiet config change. Warn users in advance, and make sure your client handles a 401 by sending the user to login instead of retrying the same dead token in a loop.
Cookie auth and token auth expire on different clocks
Sanctum has two modes and they do not share an expiry. First-party single page apps authenticated with cookies live under your session configuration: SESSION_LIFETIME and the session cookie settings. API tokens issued with createToken() live under sanctum.expiration. Changing one does nothing to the other.
I have seen teams "fix" a token lifetime complaint by changing the session lifetime, then wonder why nothing changed for their mobile users. Before you touch a config value, confirm which mode the affected client uses. If the request carries an Authorization: Bearer header, it is token auth. If it relies on the session cookie and CSRF token, it is cookie auth. Fix the clock that actually governs that client.
Expiry is not revocation, and logout must revoke
An expiry lifetime limits damage, but it does not replace revocation. If a user logs out, change their password, or reports a device stolen, the token should die immediately, not at the end of its 30 days. Sanctum makes this one line, and skipping it is the most common security gap in token setups I have reviewed:
// Log out only the current token
$request->user()->currentAccessToken()->delete();
// Log out everywhere (password change, stolen device)
$request->user()->tokens()->delete();
Wire the second form into your password change and "log out of all devices" flows. An attacker holding a copied token does not care that it expires next month if the user changed their password today and the token still works.
Expired tokens pile up unless you prune them
Setting an expiry does not delete anything. Expired tokens stay in personal_access_tokens until you remove them, and on a busy API that table grows without bound. Sanctum ships a command for exactly this, and it belongs in your scheduler:
php artisan sanctum:prune-expired
// In your scheduler
Schedule::command('sanctum:prune-expired')->daily();
Run it on a schedule, not by hand when someone notices the table is large.
Treat a 401 as a state change, not an error to retry
The client side matters as much as the server side. When a token expires or is revoked, the API returns 401. A client that retries the request, refreshes the page in a loop, or silently keeps the dead token will turn one expired session into a confusing bug report. The correct client behavior is simple: on a 401 from an authenticated endpoint, clear the stored token and send the user to login with a plain message like "Your session expired. Please sign in again."
If users report being stuck after a logout, check this first. It is the same triage instinct as how I triage a production incident: reproduce the failure and look at what the client actually received.
The takeaway
Sanctum token expiry is a small config value with outsized consequences. Set it deliberately, remember it applies retroactively to existing tokens, keep cookie and token clocks separate in your head, revoke on logout and password change, prune expired tokens on a schedule, and handle 401s cleanly on the client. Do those six things and token expiry stops being a source of surprise logouts and quiet security holes, and becomes what it should be: a boring, predictable part of your auth setup.