Skip to content
Migrating from NextAuth.js v4? Read our migration guide.
API reference
@auth/unstorage-adapter

@auth/unstorage-adapter

Official Unstorage adapter for Auth.js / NextAuth.js.

Installation

npm install unstorage @auth/unstorage-adapter

UnstorageAdapterOptions

This is the interface of the Unstorage adapter options.

Properties

accountByUserIdPrefix?

optional accountByUserIdPrefix: string;

The prefix for the accountByUserId key

accountKeyPrefix?

optional accountKeyPrefix: string;

The prefix for the account key

authenticatorKeyPrefix?

optional authenticatorKeyPrefix: string;

The prefix for the authenticator key

authenticatorUserKeyPrefix?

optional authenticatorUserKeyPrefix: string;

The prefix for the authenticator-by-user-id key

baseKeyPrefix?

optional baseKeyPrefix: string;

The base prefix for your keys

emailKeyPrefix?

optional emailKeyPrefix: string;

The prefix for the emailKey key

sessionByUserIdKeyPrefix?

optional sessionByUserIdKeyPrefix: string;

The prefix for the sessionByUserId key

sessionKeyPrefix?

optional sessionKeyPrefix: string;

The prefix for the sessionKey key

useItemRaw?

optional useItemRaw: boolean;

Use getItemRaw/setItemRaw instead of getItem/setItem.

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

userKeyPrefix?

optional userKeyPrefix: string;

The prefix for the user key

verificationTokenKeyPrefix?

optional verificationTokenKeyPrefix: string;

The prefix for the verificationToken key


defaultOptions

const defaultOptions: {
  accountByUserIdPrefix: "user:account:by-user-id:";
  accountKeyPrefix: "user:account:";
  authenticatorKeyPrefix: "authenticator:id:";
  authenticatorUserKeyPrefix: "authenticator:by-user-id:";
  baseKeyPrefix: "";
  emailKeyPrefix: "user:email:";
  sessionByUserIdKeyPrefix: "user:session:by-user-id:";
  sessionKeyPrefix: "user:session:";
  useItemRaw: false;
  userKeyPrefix: "user:";
  verificationTokenKeyPrefix: "user:token:";
};

Type declaration

accountByUserIdPrefix

accountByUserIdPrefix: string = "user:account:by-user-id:";

accountKeyPrefix

accountKeyPrefix: string = "user:account:";

authenticatorKeyPrefix

authenticatorKeyPrefix: string = "authenticator:id:";

authenticatorUserKeyPrefix

authenticatorUserKeyPrefix: string = "authenticator:by-user-id:";

baseKeyPrefix

baseKeyPrefix: string = "";

emailKeyPrefix

emailKeyPrefix: string = "user:email:";

sessionByUserIdKeyPrefix

sessionByUserIdKeyPrefix: string = "user:session:by-user-id:";

sessionKeyPrefix

sessionKeyPrefix: string = "user:session:";

useItemRaw

useItemRaw: boolean = false;

userKeyPrefix

userKeyPrefix: string = "user:";

verificationTokenKeyPrefix

verificationTokenKeyPrefix: string = "user:token:";

UnstorageAdapter()

UnstorageAdapter(storage, options): Adapter

Setup

Configure Auth.js to use the Unstorage Adapter.

pages/api/auth/[...nextauth].js
import NextAuth from "next-auth"
import GoogleProvider from "next-auth/providers/google"
import { UnstorageAdapter } from "@auth/unstorage-adapter"
import { createStorage } from "unstorage";
 
const storage = createStorage();
 
export default NextAuth({
  adapter: UnstorageAdapter(storage),
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    }),
  ],
})

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:",
  authenticatorKeyPrefix: "authenticator:id:",
  authenticatorUserKeyPrefix: "authenticator:by-user-id:",
}

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.

Example:

export default 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 default NextAuth({
  adapter: UnstorageAdapter(storage, {useItemRaw: true})
})

Parameters

ParameterType
storageStorage<StorageValue>
optionsUnstorageAdapterOptions

Returns

Adapter


hydrateDates()

hydrateDates(json): any

Parameters

ParameterType
jsonobject

Returns

any

Auth.js © Balázs Orbán and Team - 2024