npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

nuxt-biscuit

v1.0.7

Published

Lightweight Nuxt module for Laravel Sanctum cookie authentication

Downloads

12

Readme


Lightweight Nuxt module for Nuxt 3/4 that wires a Nuxt frontend to a Laravel Sanctum backend using first-party cookie authentication. Nuxt Biscuit ships a preconfigured composable, route middleware, and runtime plugin so you can log users in, out, and guard pages with only a few lines of code.

✨ Features

  • ✅ Fetches and caches the authenticated user automatically on app mount
  • 🍪 Handles Sanctum CSRF cookie flow (including decoding XSRF-TOKEN)
  • 🔐 Provides auth and guest route middleware for protected/guest pages
  • ⚙️ Exposes a fully typed useBiscuit composable with login, logout, fetchUser, and onUserChange
  • 🔁 Emits client-side hooks whenever the user session changes

📋 Requirements

  • Nuxt ^3.0.0 || ^4.0.0
  • Laravel backend configured with Sanctum and cookie-based authentication

🚀 Installation

Add the dependency to your project:

npm install nuxt-biscuit

Register the module inside your nuxt.config:

export default defineNuxtConfig({
  modules: ['nuxt-biscuit'],
  biscuit: {
    baseUrl: process.env.API_BASE_URL ?? 'http://localhost:8000',
    endpoints: {
      csrf: '/sanctum/csrf-cookie',
      login: '/login',
      logout: '/logout',
      user: '/api/user'
    },
    redirect: {
      onLogin: '/',
      onLogout: '/login',
      onAuthOnly: '/login',
      onGuestOnly: '/'
    }
  }
})

All options are optional. Values you omit fall back to the defaults shown above.

📖 Usage

Composable

Call useBiscuit() inside your components or pages to access the current user and helpers:

const { user, isGuest, isChecked, login, logout, fetchUser, onUserChange } = useBiscuit()
  • login(credentials) automatically fetches the Sanctum CSRF cookie, posts credentials, updates user state, and redirects to redirect.onLogin. The credentials object should contain email, password, and optionally remember (boolean).

Example usage:

const { login } = useBiscuit()

await login({
  email: '[email protected]',
  password: 'password',
  remember: true // optional: remember the user's session
})
  • logout() posts to the logout endpoint, clears user state, fires change hooks, and redirects to redirect.onLogout.
  • fetchUser() fetches the authenticated user and populates shared state; it safely handles guest responses.
  • onUserChange(callback) runs on the client whenever the session transitions between guest and authenticated.

Middleware

Register the bundled route middleware in your pages:

definePageMeta({
  middleware: ['auth'] // blocks guests, redirects to `redirect.onAuthOnly`
})

definePageMeta({
  middleware: ['guest'] // blocks authenticated users, redirects to `redirect.onGuestOnly`
})

Both middleware calls ensure the user is fetched exactly once per client session before performing redirects.

Programmatic Navigation Hooks

The plugin keeps user, isChecked, and hooks in a Nuxt state namespace. If you need to react to login/logout globally (e.g. to fetch extra data), use the composable hook:

const { onUserChange } = useBiscuit()

onUserChange((newUser) => {
  if (newUser) {
    // user just logged in
  } else {
    // user just logged out
  }
})

📄 License

Licensed under the MIT license.