DEV Community

Bright Agyemang
Bright Agyemang

Posted on

🚀 Introducing lara-fetch - Laravel Sanctum made SIMPLE (no tears 😭)

Okay hear me out…

Building Laravel SPAs with Sanctum shouldn’t feel like a whole ritual every time:

  • Get CSRF cookie
  • Dig up the XSRF-TOKEN cookie
  • Attach headers manually
  • Pray the request doesn’t 419 💀

So I built a tiny fetch wrapper that handles all that automatically:

lara-fetch — a lightweight client that handles Sanctum csrf-cookies for you

(with zero stress + optional oversharing aka debug mode 😌)


💡 What Problem Does It Solve?

Instead of doing something like:

await fetch('/sanctum/csrf-cookie', { credentials: 'include' })
// then extract token from cookie
// then set headers
// then hope…
Enter fullscreen mode Exit fullscreen mode

You can just:

import { laraFetch } from "lara-fetch"

laraFetch.post('/user/auth', {
  body: JSON.stringify({ email, password })
})
Enter fullscreen mode Exit fullscreen mode

Done ✅
Sanctum + CSRF ✅
No stress ✅


⚙️ One-Time Setup (Literally ONE line)

Add this in your main entry file:

import { laraFetch } from "lara-fetch"

laraFetch.configure({
  baseURL: "http://localhost:8000", // Your Laravel API URL
  debug: true // optional: console oversharing 😅,
  ... // additional config as per need
})
Enter fullscreen mode Exit fullscreen mode

And BOOM 💥 — all requests automatically handle cookies + CSRF.


🧐 Why I’m Proud of This

✅ Zero boilerplate
✅ No more “Token mismatch” rage 😤
✅ Works with:

  • Vue / Nuxt
  • React / Next
  • Even vanilla JavaScript 🫶

✅ Debug mode logs every move it makes 👀


📦 Installation

npm i lara-fetch
Enter fullscreen mode Exit fullscreen mode

or

pnpm add lara-fetch
Enter fullscreen mode Exit fullscreen mode

Yes, pnpm gang rise ✊🔥


🔗 GitHub Repo

Stars fuel open-source energy 🌟
👉 Check repo
PRs + feature ideas super welcome!


✅ Roadmap

  • Pending: Auto session refresh
  • Pending: Retry system
  • Pending: Official docs + starter templates

✨ Final Word

I built lara-fetch because I was suffering…
and I refuse to let other developers suffer too 😌

If you try it and it slaps — drop a comment 💬 and star the repo 🌟

Top comments (1)

Collapse
 
bright_agyemang_d609f542f profile image
Bright Agyemang • Edited

Drop in any limitation you find to be worked out