<?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: Harsh Oza</title>
    <description>The latest articles on DEV Community by Harsh Oza (@ozaharsh95).</description>
    <link>https://dev.to/ozaharsh95</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%2F1129288%2F4695c454-dd4b-4523-b34c-bcace92923cc.png</url>
      <title>DEV Community: Harsh Oza</title>
      <link>https://dev.to/ozaharsh95</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ozaharsh95"/>
    <language>en</language>
    <item>
      <title>Bug: cross site cookie restrictions</title>
      <dc:creator>Harsh Oza</dc:creator>
      <pubDate>Fri, 07 Aug 2026 11:43:35 +0000</pubDate>
      <link>https://dev.to/ozaharsh95/bug-cross-site-cookie-restrictions-1hd3</link>
      <guid>https://dev.to/ozaharsh95/bug-cross-site-cookie-restrictions-1hd3</guid>
      <description>&lt;h2&gt;
  
  
  What went wrong?
&lt;/h2&gt;

&lt;p&gt;When a user logged in on my deployed frontend (&lt;code&gt;https://domainA.app&lt;/code&gt;), the credentials succeeded, but they were immediately treated as unauthenticated. Protected endpoints (like &lt;code&gt;/stats/admin/*&lt;/code&gt;) failed with a &lt;code&gt;401 Unauthorized&lt;/code&gt; status code.&lt;/p&gt;

&lt;p&gt;Crucially, this behavior &lt;strong&gt;did not&lt;/strong&gt; happen on localhost. &lt;/p&gt;

&lt;h2&gt;
  
  
  Why did it happen?
&lt;/h2&gt;

&lt;p&gt;This is caused by cross-site cookie restrictions enforced by modern web browsers.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;On localhost:&lt;/strong&gt; The frontend (&lt;code&gt;http://localhost:5173&lt;/code&gt;) and backend (&lt;code&gt;http://localhost:8000&lt;/code&gt;) share the same hostname (&lt;code&gt;localhost&lt;/code&gt;). Browsers treat different ports on localhost as a secure, shared origin context, meaning cookie exchange works by default without strict rules.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;On production:&lt;/strong&gt; The frontend (&lt;code&gt;domainA.app&lt;/code&gt;) and backend (&lt;code&gt;domainB.app&lt;/code&gt;) are hosted on completely different domains (cross-site).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The Cookie Block:&lt;/strong&gt; &lt;br&gt;
When the backend tries to set the authentication session cookie (&lt;code&gt;better-auth.session_token&lt;/code&gt;), the browser blocks and rejects it because the cookie does not have the necessary flags to be sent across different domains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;SameSite=None&lt;/code&gt;: Tells the browser that this cookie is allowed to be sent on cross-site requests.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;Secure&lt;/code&gt;: Enforces that the cross-site cookie is only sent over HTTPS (which is required if &lt;code&gt;SameSite=None&lt;/code&gt; is set).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The Result:&lt;/strong&gt; &lt;br&gt;
Because the browser rejected the cookie, it was never stored. When the frontend made requests to &lt;code&gt;/stats/admin/*&lt;/code&gt;, no session cookie was included. The backend session middleware received no credentials and returned a &lt;code&gt;401 Unauthorized&lt;/code&gt; error.&lt;/p&gt;

</description>
      <category>fullstack</category>
      <category>browser</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
