<?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: pial shek</title>
    <description>The latest articles on DEV Community by pial shek (@pial_shek_09ce71ae3fbe3c1).</description>
    <link>https://dev.to/pial_shek_09ce71ae3fbe3c1</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2449714%2F405e24b0-f52a-4fb2-b6d0-3b98c67f76fc.jpg</url>
      <title>DEV Community: pial shek</title>
      <link>https://dev.to/pial_shek_09ce71ae3fbe3c1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pial_shek_09ce71ae3fbe3c1"/>
    <language>en</language>
    <item>
      <title>🍪 Cookie-Based JWT Authentication</title>
      <dc:creator>pial shek</dc:creator>
      <pubDate>Thu, 09 Apr 2026 19:17:39 +0000</pubDate>
      <link>https://dev.to/pial_shek_09ce71ae3fbe3c1/cookie-based-jwt-authentication-2477</link>
      <guid>https://dev.to/pial_shek_09ce71ae3fbe3c1/cookie-based-jwt-authentication-2477</guid>
      <description>&lt;p&gt;A few weeks ago, I was staring at a project thinking —&lt;/p&gt;

&lt;p&gt;“Where is the safest place for a frontend to store these tokens?” 🤔&lt;/p&gt;

&lt;p&gt;Every tutorial said the same thing: localStorage.&lt;br&gt;
But something felt… off. ⚠️&lt;/p&gt;

&lt;p&gt;So I started digging deeper 🔍&lt;br&gt;
And realized — if any malicious script runs on the page, localStorage is wide open.&lt;br&gt;
One XSS attack… and your tokens are gone. 💀&lt;/p&gt;

&lt;p&gt;That’s when I asked myself:&lt;br&gt;
👉 What if the frontend never touches the tokens at all?&lt;/p&gt;

&lt;p&gt;💡 That’s when I discovered Cookie-Based JWT Authentication&lt;/p&gt;

&lt;p&gt;The idea is simple but powerful:&lt;br&gt;
Instead of sending JWT tokens in the response body…&lt;br&gt;
👉 store them inside httpOnly cookies&lt;/p&gt;

&lt;p&gt;Now the browser handles everything automatically:&lt;br&gt;
🍪 Stores the token&lt;br&gt;
📤 Sends it with every request&lt;br&gt;
🙈 Keeps it hidden from JavaScript&lt;/p&gt;

&lt;p&gt;⚙️ Here’s what I built:&lt;/p&gt;

&lt;p&gt;→ User logs in → server generates access + refresh tokens 🔐&lt;br&gt;
→ Tokens go into httpOnly cookies (not the response body) 🍪&lt;br&gt;
→ Every request automatically includes the token 🚀&lt;br&gt;
→ Custom auth class reads from cookies instead of headers 🧠&lt;br&gt;
→ Server validates JWT → no DB hit, fully stateless ⚡&lt;/p&gt;

&lt;p&gt;💥 The moment it clicked for me:&lt;/p&gt;

&lt;p&gt;Session auth → server remembers you 🗂️ (stateful)&lt;br&gt;
JWT auth → server trusts a signed token ✍️ (stateless)&lt;br&gt;
Cookie-based JWT → stateless + browser-level security 🔐✨&lt;/p&gt;

&lt;p&gt;🤝 And here’s the part I didn’t expect…&lt;/p&gt;

&lt;p&gt;It made life way easier for frontend developers.&lt;/p&gt;

&lt;p&gt;Before:&lt;br&gt;
😵 Store tokens manually&lt;br&gt;
😵 Attach headers everywhere&lt;br&gt;
😵 Handle refresh logic&lt;/p&gt;

&lt;p&gt;After:&lt;br&gt;
😎 Just call APIs&lt;/p&gt;

&lt;p&gt;That’s it. No token management. No extra logic.&lt;br&gt;
The browser does the heavy lifting. 💪&lt;/p&gt;

&lt;p&gt;But let’s be real — no system is perfect ⚖️&lt;/p&gt;

&lt;p&gt;⚠️ CSRF is back → need proper protection (sameSite / CSRF tokens)&lt;br&gt;
🌍 Cross-domain setups can get tricky&lt;br&gt;
📱 Mobile apps prefer headers over cookies&lt;br&gt;
⏳ Still can't instantly invalidate a stateless token without a blacklist.&lt;/p&gt;

&lt;p&gt;🚀 Despite all that…&lt;/p&gt;

&lt;p&gt;This approach felt like a big upgrade in how I design authentication systems.&lt;/p&gt;

&lt;p&gt;✔️ Secure by default&lt;br&gt;
✔️ Stateless and scalable&lt;br&gt;
✔️ Clean for frontend devs&lt;/p&gt;

&lt;p&gt;And most importantly…&lt;br&gt;
It helped me truly understand what’s happening under the hood 🧠&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>security</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>🛒 Have you ever wondered… how eCommerce sites remember your cart even without login?</title>
      <dc:creator>pial shek</dc:creator>
      <pubDate>Tue, 03 Mar 2026 15:50:48 +0000</pubDate>
      <link>https://dev.to/pial_shek_09ce71ae3fbe3c1/have-you-ever-wondered-how-ecommerce-sites-remember-your-cart-even-without-login-2bdk</link>
      <guid>https://dev.to/pial_shek_09ce71ae3fbe3c1/have-you-ever-wondered-how-ecommerce-sites-remember-your-cart-even-without-login-2bdk</guid>
      <description>&lt;p&gt;You visit a site.&lt;br&gt;
Add products to cart.&lt;br&gt;
Close the browser.&lt;br&gt;
Come back after 3 days…&lt;/p&gt;

&lt;p&gt;And BOOM 💥 your cart items are still there.&lt;/p&gt;

&lt;p&gt;How?&lt;/p&gt;

&lt;p&gt;Let’s break it down 👇&lt;/p&gt;

&lt;p&gt;The naive answer: Session IDs&lt;br&gt;
When you visit a website, the server assigns you a Session ID — a temporary identifier to track your activity. But here's the problem:&lt;br&gt;
Session IDs are temporary. Close the browser, reopen it — new session, new ID. The cart is gone. That's not the experience users expect.&lt;br&gt;
So how do production systems solve this?&lt;/p&gt;

&lt;p&gt;The real solution: Persistent Guest Identity via Cookies + Server-Side Storage&lt;br&gt;
Here's the architecture that actually works:&lt;br&gt;
*&lt;em&gt;Step 1 *&lt;/em&gt;— Generate a Persistent Guest ID&lt;br&gt;
On your first visit, the frontend generates a UUID (e.g. crypto.randomUUID()) and stores it in a cookie with a long expiry (30–90 days). This ID persists across sessions, browser closes, and revisits.&lt;br&gt;
Step 2 — Lazy Guest User Creation&lt;br&gt;
When a guest adds a product to the cart, the backend receives that UUID and runs a simple check:&lt;/p&gt;

&lt;p&gt;→ Does a guest user with this ID exist?&lt;br&gt;
→ If NO → create a guest user record tied to this UUID&lt;br&gt;
→ If YES → fetch that existing guest user&lt;/p&gt;

&lt;p&gt;No login. No email. Just a UUID as the identity anchor.&lt;/p&gt;

&lt;p&gt;Step 3 — Cart &amp;amp; Cart Items&lt;br&gt;
Once the guest user is resolved:&lt;/p&gt;

&lt;p&gt;→ Does this user have an active cart?&lt;br&gt;
→ If NO → create a cart, add the item&lt;br&gt;
→ If YES → append the new item to the existing cart&lt;br&gt;
This means any number of products can be added across multiple visits — all seamlessly linked to that one UUID sitting quietly in the browser cookie.&lt;/p&gt;

&lt;p&gt;Step 4 — Handling the Cookie Security Concerns&lt;br&gt;
Storing sensitive cart data directly in cookies is risky (size limits, client-side tampering, XSS exposure). That's why we only store the UUID in the cookie — nothing else. All actual cart data lives safely on the server/database.&lt;/p&gt;

&lt;p&gt;Step 5 — Cleanup with a Scheduled Job&lt;br&gt;
Not every guest will ever check out. Over time, your database fills up with abandoned guest carts. The solution? A cron job / scheduler that runs periodically (daily or weekly) and:&lt;/p&gt;

&lt;p&gt;→ Deletes guest users whose carts haven't been touched in X days (e.g. 30 days)&lt;br&gt;
→ Cascades deletion to their cart and cart items&lt;/p&gt;

&lt;p&gt;This keeps your database lean without manual intervention.&lt;/p&gt;

&lt;p&gt;This small architectural decision improves:&lt;/p&gt;

&lt;p&gt;✔️ User Experience&lt;br&gt;
✔️ Conversion Rate&lt;br&gt;
✔️ Abandoned Cart Recovery&lt;br&gt;
✔️ Data Tracking&lt;/p&gt;

&lt;p&gt;Sometimes the best engineering is invisible to users.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
