binarcode/laravel-stateless-session
Laravel Stateless Session stores session data in encrypted client-side cookies instead of server-side storage, helping APIs and horizontally scaled apps stay stateless while keeping Laravel’s familiar session interface.
The package solves one narrow problem: it exposes a csrf-header route that returns a fresh CSRF token in a response header so a JavaScript frontend can bootstrap Laravel’s CSRF protection without rendering a Blade form.
php artisan route:list | grep csrf-header
php artisan vendor:publish --tag=csrf-header-config
The key settings to look for are the route path, the header name (commonly X-CSRF-TOKEN), and the middleware stack applied to the route.curl -i -c cookies.txt http://localhost/csrf-header
You should see the token in the response header and any session cookie set by the web middleware group.The first real use case is an SPA or Inertia app that needs to make state-changing requests. On app boot, fetch the token once, store it, and attach it to subsequent requests. The 1.1.0 release is relevant here because the new custom middleware support lets you put authentication, throttling, or tenancy checks in front of that token endpoint without forking the package.
Axios bootstrap pattern. Fetch the token before the first mutating request and set it as a default header:
const { data, headers } = await axios.get('/csrf-header', {
withCredentials: true,
});
axios.defaults.headers.common['X-CSRF-TOKEN'] =
headers['x-csrf-token'];
If your client uses Laravel’s encrypted XSRF-TOKEN cookie convention, read the cookie instead and send it as X-XSRF-TOKEN. Pick one convention and use it consistently.
Custom middleware for the token route (new in 1.1.0). The package now lets you attach your own middleware to the csrf-header route. This is the main extension point for day-to-day use:
auth or auth:sanctum so anonymous clients cannot mint tokens.throttle:csrf-token to prevent token-endpoint hammering.Configure this through the package config’s middleware array, or by overriding the route definition if the package exposes that option. Keep session-related middleware (StartSession, ShareErrorsFromSession, etc.) ahead of your custom middleware if it reads or writes session state.
Authenticated token refresh. Issue a fresh token after login, logout, and role changes. A common pattern is to call the endpoint again after the auth state changes and replace the stored token. This avoids stale-token 419 responses after session regeneration.
Testing. Assert both the status and the header in feature tests:
$this->get('/csrf-header')
->assertOk()
->assertHeader('X-CSRF-TOKEN');
For middleware changes, add tests that assert unauthenticated requests are rejected and throttled requests return 429.
Route and config caching. If you deploy with route:cache and config:cache, add the token endpoint to your deployment smoke tests. Middleware and route-path changes made through config will not take effect until caches are rebuilt.
withCredentials: true (Axios) or credentials: 'include' (fetch). Without it, the token will be issued against a different session or no session at all.Access-Control-Expose-Headers: X-CSRF-TOKEN to your CORS configuration, and allow credentials.Cache-Control: no-store in your custom middleware or at the web server layer.web middleware in the package config can start the session twice or produce inconsistent cookies. Prefer appending your middleware to the existing stack rather than replacing it.route:list, inspect the raw response headers with curl -i, verify the session driver is not array, check config:cache after config edits, and confirm the frontend is actually sending the header name the server expects.How can I help you explore Laravel packages today?