Skip to content
Migrating from NextAuth.js v4? Read our migration guide.
Getting Started
Adapters
Unstorage

Unstorage Adapter

Resources

Setup

Installation

npm install unstorage @auth/unstorage-adapter

Configuration

./auth.ts
import NextAuth from "next-auth"
import { UnstorageAdapter } from "@auth/unstorage-adapter"
import { createStorage } from "unstorage"
 
const storage = createStorage()
 
export const { handlers, auth, signIn, signOut } = NextAuth({
  adapter: UnstorageAdapter(storage),
  providers: [],
})

Advanced usage

Using multiple apps with a single storage

If you have multiple Auth.js connected apps using the same storage, you need different key prefixes for every app.

You can change the prefixes by passing an options object as the second argument to the adapter factory function.

The default values for this object are:

const defaultOptions = {
  baseKeyPrefix: "",
  accountKeyPrefix: "user:account:",
  accountByUserIdPrefix: "user:account:by-user-id:",
  emailKeyPrefix: "user:email:",
  sessionKeyPrefix: "user:session:",
  sessionByUserIdKeyPrefix: "user:session:by-user-id:",
  userKeyPrefix: "user:",
  verificationTokenKeyPrefix: "user:token:",
}

Usually changing the baseKeyPrefix should be enough for this scenario, but for more custom setups, you can also change the prefixes of every single key.

import NextAuth from "next-auth"
import { UnstorageAdapter } from "@auth/unstorage-adapter"
import { createStorage } from "unstorage"
 
const storage = createStorage()
 
export const { handlers, auth, signIn, signOut } = NextAuth({
  adapter: UnstorageAdapter(storage, { baseKeyPrefix: "app2:" }),
})

Using getItemRaw/setItemRaw instead of getItem/setItem

If you are using storage that supports JSON, you can make it use getItemRaw/setItemRaw instead of getItem/setItem.

This is an experimental feature. Please check unjs/unstorage#142 for more information.

You can enable this functionality by passing useItemRaw: true (default: false) in the options object as the second argument to the adapter factory function.

export const { handlers, auth, signIn, signOut } = NextAuth({
  adapter: UnstorageAdapter(storage, { useItemRaw: true }),
})
Auth.js © Balázs Orbán and Team - 2024