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

@dommidev10/nuxt-auth

v0.11.0

Published

My new Nuxt module

Downloads

38

Readme

Nuxt Auth Module

npm version npm downloads License Nuxt

A flexible and fully-typed authentication module for Nuxt 3.
Supports token-based auth, refresh tokens, session management, route protection, and more.

Inspired by sidebase/nuxt-auth
This module aims to provide a minimal, type-safe, and backend-agnostic Nuxt 3 auth solution for token and refresh token flows.


Features

  • 🔒 Token-based authentication (access & refresh tokens)
  • 🍪 Secure cookie management (configurable)
  • 🔁 Automatic token refresh (with optional refresh-on-focus)
  • 🛡 Route protection via middleware (global or per-page)
  • 🧑‍💻 Type-safe session object (customizable)
  • ⚡️ Simple composable API: useAuth()
  • 🧩 Easy integration with any backend

Quick Setup

Install the module in your Nuxt application:

npx nuxi module add @dommidev10/nuxt-auth
# or
npm install @dommidev10/nuxt-auth

Add it to your nuxt.config.ts:

export default defineNuxtConfig({
  modules: ['@dommidev10/nuxt-auth'],
  auth: {
    baseURL: 'http://localhost:4000/auth',
    endpoints: {
      signIn: { path: '/login', method: 'post' },
      signOut: { path: '/logout', method: 'post' },
      getSession: { path: '/me', method: 'get' },
    },
    token: {
      signInResponseTokenPointer: '/access_token',
      type: 'Bearer',
      cookieName: 'auth.token',
      headerName: 'Authorization',
      maxAgeInSeconds: 86400,
      sameSiteAttribute: 'lax',
    },
    refresh: {
      endpoint: { path: '/refresh', method: 'post' },
      refreshOnlyToken: true,
      token: {
        signInResponseRefreshTokenPointer: '/refresh_token',
        refreshResponseTokenPointer: '/access_token',
        refreshRequestTokenPointer: '/refresh_token',
        cookieName: 'auth.refresh_token',
        maxAgeInSeconds: 604800,
        sameSiteAttribute: 'lax',
      },
    },
    pages: {
      login: '/auth/login',
    },
    session: {
      dataType: {
        id: 'string',
        name: 'string',
      },
    },
    globalMiddleware: true, // Set to false to use per-page middleware
    refreshOnFocusChanged: true, // Only refresh if token is expired
  },
})

Usage

1. Use the composable in your app

const { session, signIn, signOut, refresh, getSession, token } = useAuth()
  • session: Reactive user session object (typed)
  • signIn(credentials): Log in and set tokens/session
  • signOut(): Log out and clear tokens/session
  • refresh(): Refresh the access token using the refresh token
  • getSession(): Fetch the current session from your API
  • token: Reactive access token (from cookie)

2. Protecting routes

Global protection (default)

All routes are protected by default. Unauthenticated users are redirected to your login page.

Per-page protection

Set globalMiddleware: false in your config, then add the middleware to any page or layout:

export default defineNuxtPage({
  middleware: ['auth'],
})

3. Customizing the session type

Define your session fields in session.dataType in your config.
This will generate a TypeScript interface for autocompletion in your app.

session: {
  dataType: {
    id: 'string',
    name: 'string',
    email: 'string'
  }
}

How it works

  • Tokens are stored in cookies with your chosen options (secure, httpOnly, sameSite, etc).
  • Session is fetched from your API and kept in a reactive state.
  • Middleware checks authentication before each route navigation.
  • Refresh-on-focus (if enabled) only calls the refresh endpoint if the token is expired, to avoid unnecessary backend calls.

Minimal Example

This module is intentionally minimal and backend-agnostic.
You only need to provide the endpoints and token pointers that match your backend API.

auth: {
  baseURL: 'http://localhost:4000/auth',
  endpoints: {
    signIn: { path: '/login', method: 'post' },
    signOut: { path: '/logout', method: 'post' },
    getSession: { path: '/me', method: 'get' }
  },
  token: {
    signInResponseTokenPointer: '/access_token',
    cookieName: 'auth.token',
    headerName: 'Authorization',
    maxAgeInSeconds: 86400,
    sameSiteAttribute: 'lax'
  },
  refresh: {
    endpoint: { path: '/refresh', method: 'post' },
    token: {
      signInResponseRefreshTokenPointer: '/refresh_token',
      refreshResponseTokenPointer: '/access_token',
      refreshRequestTokenPointer: '/refresh_token',
      cookieName: 'auth.refresh_token',
      maxAgeInSeconds: 604800,
      sameSiteAttribute: 'lax'
    }
  },
  pages: { login: '/auth/login' },
  session: { dataType: { id: 'string', name: 'string' } }
}

FAQ

Q: Can I use this with any backend?
A: Yes! Just configure the endpoints and token pointers to match your API.

Q: How do I add public pages?
A: Set globalMiddleware: false and only add the middleware to protected pages.

Q: How do I add extra fields to the session?
A: Add them to session.dataType in your config.

Q: Does refreshOnFocusChanged spam my backend?
A: No, it only triggers a refresh if the token is expired.


Contribution

# Install dependencies
npm install

# Generate type stubs
npm run dev:prepare

# Develop with the playground
npm run dev

# Build the playground
npm run dev:build

# Run ESLint
npm run lint

# Run Vitest
npm run test
npm run test:watch

# Release new version
npm run release

License

MIT