Skip to content

kiki-kanri/nuxt-session

Repository files navigation

@kikiutils/nuxt-session

npm version npm downloads License Nuxt

Easy-to-use nuxt server-side session.

Features

  • 🔄 Continuous session between requests using a cookie or header
  • 🌐 Available in server-side middleware
  • 💾 Auto-save session
  • 📦 Store session data using a cookie, header, or unjs/unstorage drivers
  • 🔒 Optional strict IP validation to ensure session security
  • 🛠️ TypeScript support

Environment Requirements

  • Nuxt version 3.9.x or higher, but below 4.x
  • Node.js version 18 or higher

Installation

  1. Add dependency (example using pnpm).
pnpm add @kikiutils/nuxt-session

You can also use yarn, npm, or bun to add the dependency.

  1. Add this module to the modules field in nuxt.config.ts:
export default defineNuxtConfig({
    modules: ['@kikiutils/nuxt-session']
});
  1. Create a session.d.ts file in the server folder under the project directory and and add the following code:
declare module '@kikiutils/nuxt-session' {
    interface H3EventContextSession {
        // Define the session data here.
    }
}

export {};

That's it! You're ready to use sessions in your Nuxt app. Check out the configuration and usage instructions below ✨.

Configuration

Configure using nuxtSession in nuxt.config.ts.

export default defineNuxtConfig({
    nuxtSession: {
        // Configure options here
    }
});

This package uses @kikiutils/nitro-session as its core. For related configuration instructions, please refer to this link.

Except for the configuration method mentioned above, all other options remain the same.

Usage

Please refer to this link for related usage.

Important

This package is intended for use in the server folder of Nuxt. For more information about this folder, please see this link.

Only requests with paths starting with /api will handle sessions.

Server Runtime Utils

Please refer to this link for related server runtime utils.

License

MIT License