.env Secret & Syntax Checker · Laravel Sanctum / SPA

Laravel Sanctum & CORS .env Validator (Align Stateful Domains & Cookies)

Lint your Laravel Sanctum / SPA .env file locally in your browser to catch secret leaks, syntax truncation, and missing .env.example keys. Copy an auto-fixed .env or sanitized template below.

Written & verified by Chathura Prabashana · Updated November 2026

1. Paste .env or load a test scenario2. Inspect live diagnostics3. Copy cleaned config
Quick-Test Scenarios:

1. Source .env Input

.env.example Parity Diff8 matched key(s)
Missing Required Keys:
SESSION_SAME_SITE
Undocumented Keys:
SESSION_DRIVER
APP_ENV=production
APP_KEY=base64:K9x8mP2vQ5nL7rT1wZ4cB6fH0jD3sA8eG2yU5iO9pM0=
APP_DEBUG=true
APP_URL=http://localhost:8000
FRONTEND_URL=http://localhost:5173
SANCTUM_STATEFUL_DOMAINS=localhost:5173
SESSION_DOMAIN=localhost
SESSION_DRIVER=cookie
SESSION_SECURE_COOKIE=false
Critical
1
Warnings
4
Valid Keys
5
Missing Keys
1

Line-by-Line Diagnostics

Runs locally in your browser. Pasted .env values are never uploaded or saved to localStorage.

Typical Runtime Error (Laravel Sanctum / SPA .env Failure)

When syntax errors, unquoted special characters, missing keys, or public-prefix secret leaks slip into a Laravel Sanctum / SPA .env file, your runtime or audit pipeline surfaces an error like the output below:

Runtime Exception / Audit Output

HTTP 419 Page Expired (CSRF token mismatch) — Browser dropped laravel_session & XSRF-TOKEN cookies because SESSION_DOMAIN and SANCTUM_STATEFUL_DOMAINS did not match the production HTTPS origin.

How Laravel Sanctum / SPA Loads .env Files, Precedence & Security Boundaries

Laravel Sanctum's SPA cookie authentication depends on five tightly synchronized .env variables: APP_URL, FRONTEND_URL, SANCTUM_STATEFUL_DOMAINS, SESSION_DOMAIN, and SESSION_SECURE_COOKIE. Misconfiguring even one character; such as leaving http://localhost:5173 in SANCTUM_STATEFUL_DOMAINS during production deployment, putting https:// inside SANCTUM_STATEFUL_DOMAINS, or forgetting the leading dot in SESSION_DOMAIN=.example.com; breaks stateful cookie sessions immediately.

A particularly confusing Sanctum bug happens when engineers move from localhost to production subdomains (app.example.com calling api.example.com). Locally, SANCTUM_STATEFUL_DOMAINS requires the port (localhost:5173) and SESSION_DOMAIN is localhost. In production over HTTPS port 443, SANCTUM_STATEFUL_DOMAINS must contain ONLY the bare hostname (app.example.com, with no https:// and no trailing slash), while SESSION_DOMAIN must include the leading dot (.example.com) and SESSION_SECURE_COOKIE=true.

If FRONTEND_URL has a trailing slash (https://app.example.com/), config/cors.php fails exact browser Origin comparison. Meanwhile, if APP_DEBUG=true slips into production alongside SESSION_DRIVER=cookie, debug output can expose encrypted payload details and misconfigured cookie flags.

Validating your Laravel Sanctum .env file with this browser-only checker flags leftover localhost domains in production mode, verifies APP_KEY cipher length, checks for missing SESSION_SAME_SITE or SESSION_SECURE_COOKIE keys against .env.example, and produces clean, quoted output ready for deployment.

After updating any Sanctum .env variables on staging or production, remember to run php artisan config:clear && php artisan config:cache and clear existing browser cookies on your test tab so stale session cookies from the old SESSION_DOMAIN do not linger.

Maintaining separate, clearly documented .env templates for local development (localhost:5173 + SESSION_DOMAIN=localhost) and HTTPS production subdomains (app.example.com + SESSION_DOMAIN=.example.com + SESSION_SECURE_COOKIE=true) prevents the most frustrating Laravel Sanctum 419 and 401 debugging loops. Whenever deploying Sanctum cookie changes, audit APP_URL, FRONTEND_URL, SANCTUM_STATEFUL_DOMAINS, and SESSION_DOMAIN together using the validator above, rebuild Laravel's config cache, and verify both Set-Cookie and Access-Control-Allow-Credentials headers in browser DevTools.

Step-by-Step Laravel Sanctum / SPA .env Configuration & Hardening Guide

Apply these four concrete safeguards to eliminate silent dotenv truncation, prevent secret leaks, and keep local and production environments synchronized.

  1. 1Align Production Sanctum Subdomain Variables in Laravel .env

    .env (production)

    Use bare hostnames (no scheme, no trailing slash) in SANCTUM_STATEFUL_DOMAINS, share cookies across sibling subdomains with a leading-dot SESSION_DOMAIN, and require HTTPS cookies.

    .env (production)dotenv
    APP_ENV=production
    APP_DEBUG=false
    APP_URL=https://api.example.com
    FRONTEND_URL=https://app.example.com
    
    SANCTUM_STATEFUL_DOMAINS=app.example.com
    SESSION_DOMAIN=.example.com
    SESSION_DRIVER=redis
    SESSION_SECURE_COOKIE=true
    SESSION_SAME_SITE=lax
  2. 2Wire FRONTEND_URL Cleanly Into config/cors.php With Credentials Enabled

    config/cors.php

    Trim any accidental trailing slashes defensively in config/cors.php and keep supports_credentials set to true for Sanctum SPAs.

    config/cors.phpphp
    return [
        'paths' => ['api/*', 'sanctum/csrf-cookie', 'login', 'logout'],
        'allowed_methods' => ['*'],
        'allowed_origins' => [rtrim(env('FRONTEND_URL', 'http://localhost:5173'), '/')],
        'allowed_headers' => ['Content-Type', 'Accept', 'X-XSRF-TOKEN', 'X-Requested-With'],
        'supports_credentials' => true,
    ];
  3. 3Keep Local Development Sanctum .env Consistent Across localhost

    .env (local development)

    Include the Vite or Next.js port inside SANCTUM_STATEFUL_DOMAINS locally and never mix localhost with 127.0.0.1.

    .env (local development)dotenv
    APP_URL=http://localhost:8000
    FRONTEND_URL=http://localhost:5173
    SANCTUM_STATEFUL_DOMAINS=localhost:5173
    SESSION_DOMAIN=localhost
    SESSION_SECURE_COOKIE=false
  4. 4Clear Config Cache and Inspect Set-Cookie Headers With curl

    terminal

    Rebuild Laravel config cache and inspect the live /sanctum/csrf-cookie response headers to confirm Domain, Secure, and SameSite flags.

    terminalbash
    php artisan config:clear && php artisan config:cache
    curl -i https://api.example.com/sanctum/csrf-cookie -H "Origin: https://app.example.com"

Common Laravel Sanctum / SPA .env Pitfalls

Check these three framework-specific traps before deploying updated environment variables to staging or production.

Pitfall 1

Putting https:// Inside SANCTUM_STATEFUL_DOMAINS

Sanctum strips protocol schemes when comparing request Origin/Referer hosts against SANCTUM_STATEFUL_DOMAINS. Writing https://app.example.com instead of app.example.com prevents Sanctum from activating stateful session middleware.

Pitfall 2

Leaving SESSION_DOMAIN=localhost in Production

Browsers reject Set-Cookie headers whose Domain attribute does not match the responding host. Leaving SESSION_DOMAIN=localhost in production causes browsers to silently discard every session and XSRF-TOKEN cookie.

Pitfall 3

Deploying Frontend and API on Different Top-Level Domains With SameSite=lax

SESSION_DOMAIN=.example.com only works when both SPA and API share the same root domain (app.example.com + api.example.com). Separate root domains (vercel.app + company.com) are blocked as third-party cookies by Safari ITP.

Related Tools

Pair Your Validated .env With Webhook HMAC, Docker Compose & CORS Tools

Now that your environment variables are syntax-clean and leak-free, verify webhook raw-body HMAC signatures, container healthchecks, and browser cross-origin rules:

Frequently Asked Questions

Should SESSION_DOMAIN start with a dot (.example.com) for Sanctum SPAs?
Yes; setting SESSION_DOMAIN=.example.com allows cookies set by api.example.com to be sent back by app.example.com and any sibling subdomain cleanly.
Can SANCTUM_STATEFUL_DOMAINS list multiple frontend domains?
Yes. Separate multiple hostnames with commas and zero spaces: SANCTUM_STATEFUL_DOMAINS=app.example.com,admin.example.com.
Why does /sanctum/csrf-cookie work locally but fail with 419 behind Nginx HTTPS?
Ensure SESSION_SECURE_COOKIE=true, TrustProxies is configured in bootstrap/app.php so Laravel detects HTTPS behind Nginx, and Axios sets both withCredentials: true and withXSRFToken: true.
Where should I verify the browser CORS headers matching these Sanctum .env values?
Use our companion Laravel Sanctum SPA Cookie Auth CORS Simulator linked below to test exact preflight X-XSRF-TOKEN and Access-Control-Allow-Credentials headers.
Browse all 10 .env guides →