<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nuxavel</title>
    <description>The latest articles on DEV Community by Nuxavel (@nuxavel).</description>
    <link>https://dev.to/nuxavel</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4053610%2F948e2abd-0796-4948-817d-9e60e7a3bbd5.jpeg</url>
      <title>DEV Community: Nuxavel</title>
      <link>https://dev.to/nuxavel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nuxavel"/>
    <language>en</language>
    <item>
      <title>Laravel Sanctum with Nuxt SSR: why the session disappears on the server</title>
      <dc:creator>Nuxavel</dc:creator>
      <pubDate>Wed, 29 Jul 2026 17:21:55 +0000</pubDate>
      <link>https://dev.to/nuxavel/laravel-sanctum-with-nuxt-ssr-why-the-session-disappears-on-the-server-4eno</link>
      <guid>https://dev.to/nuxavel/laravel-sanctum-with-nuxt-ssr-why-the-session-disappears-on-the-server-4eno</guid>
      <description>&lt;p&gt;It works in the browser and 401s during server rendering. The cause is that your SSR fetch is a different HTTP client entirely — one with no cookies, no browser Origin, and often no route to the URL you configured.&lt;/p&gt;

&lt;h2&gt;
  
  
  The symptom
&lt;/h2&gt;

&lt;p&gt;You have a Laravel API using Sanctum's SPA cookie authentication and a Nuxt frontend on a separate origin. In the browser everything is fine — you log in, the session cookie is set, authenticated requests succeed.&lt;/p&gt;

&lt;p&gt;Then you fetch data during server rendering and get &lt;code&gt;401 Unauthenticated&lt;/code&gt;. Reload with JavaScript disabled and the page is empty. Hydrate, and the same request suddenly works.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why it happens
&lt;/h2&gt;

&lt;p&gt;Sanctum's SPA mode is &lt;strong&gt;cookie and session authentication&lt;/strong&gt;, not tokens. It works because the browser attaches the session cookie automatically to same-site requests.&lt;/p&gt;

&lt;p&gt;During SSR there is no browser. Your &lt;code&gt;$fetch&lt;/code&gt; runs inside the Nitro server process, which is a plain HTTP client: no cookie jar, no origin, no knowledge that a user is signed in. Laravel receives an anonymous request and answers accordingly. It is not a misconfiguration so much as a different client making the call.&lt;/p&gt;

&lt;p&gt;Three things are missing on that server-side request, and all three matter:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;strong&gt;session cookie&lt;/strong&gt;, which the browser sent to Nuxt but Nuxt never passed on.&lt;/li&gt;
&lt;li&gt;An &lt;strong&gt;Origin header&lt;/strong&gt; Sanctum recognises as a stateful domain.&lt;/li&gt;
&lt;li&gt;Often, a &lt;strong&gt;reachable URL&lt;/strong&gt; — the address the browser uses may not resolve from inside your server.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Forward the cookie during SSR
&lt;/h2&gt;

&lt;p&gt;Nuxt gives you the incoming request's headers via &lt;code&gt;useRequestHeaders&lt;/code&gt;. The fix is to pass the cookie through on server-side calls only:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Browser requests carry cookies themselves. Server-side ones carry nothing,&lt;/span&gt;
&lt;span class="c1"&gt;// so the incoming request's Cookie header has to be forwarded by hand.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;forwarded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;meta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;server&lt;/span&gt;
  &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nf"&gt;useRequestHeaders&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cookie&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt; &lt;span class="na"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;useRequestURL&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;origin&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;$fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;baseURL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;credentials&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;include&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;Accept&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;application/json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;forwarded&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;origin&lt;/code&gt; matters as much as the cookie. Sanctum decides whether a request is "stateful" by comparing the referer or origin against &lt;code&gt;SANCTUM_STATEFUL_DOMAINS&lt;/code&gt;. A server-to-server call has no Origin at all, so even with a valid session cookie Sanctum treats it as a token request and ignores the session. Setting it to &lt;code&gt;useRequestURL().origin&lt;/code&gt; — the public URL of your own frontend — is what makes the check pass.&lt;/p&gt;

&lt;h2&gt;
  
  
  The second trap: the server can't reach your API URL
&lt;/h2&gt;

&lt;p&gt;This one costs people an extra evening, because the cookie fix looks correct and the request still fails — now with a connection error rather than a 401.&lt;/p&gt;

&lt;p&gt;Your public API base might be &lt;code&gt;http://localhost:8000&lt;/code&gt; in development or &lt;code&gt;https://api.example.com&lt;/code&gt; in production. Inside a container, &lt;code&gt;localhost&lt;/code&gt; is that container, not your API. And a server sitting next to the API has no reason to route back out through the public internet to reach it.&lt;/p&gt;

&lt;p&gt;So the base URL has to differ by environment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Server-side calls use an internal address; the browser uses the public one.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;baseURL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;meta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;server&lt;/span&gt;
  &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apiBaseServer&lt;/span&gt;   &lt;span class="c1"&gt;// e.g. http://app:8000 on the Docker network&lt;/span&gt;
  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apiBase&lt;/span&gt;  &lt;span class="c1"&gt;// e.g. https://api.example.com&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Nuxt's &lt;code&gt;runtimeConfig&lt;/code&gt;, anything outside &lt;code&gt;public&lt;/code&gt; is server-only — which is exactly the distinction you want here.&lt;/p&gt;

&lt;h2&gt;
  
  
  CSRF, which is a separate problem
&lt;/h2&gt;

&lt;p&gt;Sanctum's SPA mode also enforces CSRF on state-changing requests. The flow is: call &lt;code&gt;/sanctum/csrf-cookie&lt;/code&gt; once, Laravel sets an &lt;code&gt;XSRF-TOKEN&lt;/code&gt; cookie, and every subsequent POST, PUT or DELETE echoes it back in an &lt;code&gt;X-XSRF-TOKEN&lt;/code&gt; header.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;xsrf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useCookie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;XSRF-TOKEN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;

&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// URL-decoded: Laravel writes it encoded, and comparing the raw&lt;/span&gt;
  &lt;span class="c1"&gt;// value against the decoded session token fails every time.&lt;/span&gt;
  &lt;span class="p"&gt;...(&lt;/span&gt;&lt;span class="nx"&gt;xsrf&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;X-XSRF-TOKEN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;decodeURIComponent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;xsrf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{}),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That &lt;code&gt;decodeURIComponent&lt;/code&gt; is the detail behind a great many "Sanctum 419 Page Expired" reports. The cookie is URL-encoded; the comparison is not.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Laravel side
&lt;/h2&gt;

&lt;p&gt;Four pieces of configuration have to agree. Any one of them wrong produces the same 401.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Stateful middleware
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// bootstrap/app.php&lt;/span&gt;
&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;withMiddleware&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Middleware&lt;/span&gt; &lt;span class="nv"&gt;$middleware&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$middleware&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;statefulApi&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Stateful domains
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;SANCTUM_STATEFUL_DOMAINS&lt;/code&gt; lists the frontend origins allowed to authenticate by session. Include the port in development — &lt;code&gt;localhost:3000&lt;/code&gt; and &lt;code&gt;localhost&lt;/code&gt; are different entries.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. CORS with credentials
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// config/cors.php&lt;/span&gt;
&lt;span class="s1"&gt;'paths'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'api/*'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'sanctum/csrf-cookie'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'broadcasting/auth'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="s1"&gt;'allowed_origins'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;explode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;','&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;env&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'FRONTEND_URLS'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'http://localhost:3000'&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
&lt;span class="s1"&gt;'supports_credentials'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;supports_credentials&lt;/code&gt; must be &lt;code&gt;true&lt;/code&gt; or the browser discards the cookie. &lt;code&gt;sanctum/csrf-cookie&lt;/code&gt; must be in &lt;code&gt;paths&lt;/code&gt; or the CSRF request itself fails CORS. And &lt;code&gt;allowed_origins&lt;/code&gt; cannot be &lt;code&gt;*&lt;/code&gt; when credentials are enabled — the browser rejects that combination outright.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Session cookie domain
&lt;/h3&gt;

&lt;p&gt;This is the one that bites in production. A cookie set on &lt;code&gt;api.example.com&lt;/code&gt; is not sent to &lt;code&gt;app.example.com&lt;/code&gt; unless &lt;code&gt;SESSION_DOMAIN&lt;/code&gt; is the shared parent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;SESSION_DOMAIN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;.example.com
&lt;span class="nv"&gt;SESSION_SECURE_COOKIE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;true
&lt;/span&gt;&lt;span class="nv"&gt;SESSION_SAME_SITE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;lax
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which carries a real architectural constraint: &lt;strong&gt;Sanctum's SPA mode requires both applications to share a parent domain.&lt;/strong&gt; A frontend on &lt;code&gt;myapp.vercel.app&lt;/code&gt; talking to an API on &lt;code&gt;api.example.com&lt;/code&gt; cannot use session cookies at all — no configuration fixes it, because there is no shared domain for the cookie to live on. Put the frontend on a subdomain of the same registrable domain, or use tokens instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checklist
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Symptom&lt;/th&gt;
&lt;th&gt;Usual cause&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;401 during SSR, fine in the browser&lt;/td&gt;
&lt;td&gt;Cookie header not forwarded&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;401 with the cookie forwarded&lt;/td&gt;
&lt;td&gt;No Origin header, so not treated as stateful&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Connection refused during SSR only&lt;/td&gt;
&lt;td&gt;Server using the browser-facing base URL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;419 on POST&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;XSRF-TOKEN&lt;/code&gt; not URL-decoded&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cookie never set at all&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;supports_credentials&lt;/code&gt; false, or wildcard origin&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Works locally, fails in production&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;SESSION_DOMAIN&lt;/code&gt; not the shared parent&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;This is the configuration &lt;a href="https://nuxavel.com" rel="noopener noreferrer"&gt;Nuxavel&lt;/a&gt; ships with, tested end to end — a Laravel 13 API and a Nuxt 4 SSR frontend, with the cookie forwarding above in a single &lt;code&gt;useApi&lt;/code&gt; composable. If you would rather see it working than read about it, the &lt;a href="https://demo.nuxavel.com" rel="noopener noreferrer"&gt;live demo&lt;/a&gt; is a real deployment with published sign-in details.&lt;/p&gt;

&lt;p&gt;Either way, the code above is the whole fix. It is not hidden behind anything.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>nuxt</category>
      <category>vue</category>
      <category>php</category>
    </item>
  </channel>
</rss>
