@auth/unstorage-adapter
Official Unstorage adapter for Auth.js / NextAuth.js.
Installationβ
- npm
- Yarn
- pnpm
npm install unstorage @auth/unstorage-adapter
yarn add unstorage @auth/unstorage-adapter
pnpm add unstorage @auth/unstorage-adapter
UnstorageAdapter()β
UnstorageAdapter(storage, options): Adapter
Setupβ
Configure Auth.js to use the Unstorage Adapter:
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:",
}
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.
Example:
export default NextAuth({
...
adapter: UnstorageAdapter(storage, {useItemRaw: true})
...
})
Parametersβ
βͺ storage: Storage
< StorageValue
>
βͺ options: UnstorageAdapterOptions
= {}
Returnsβ
Adapter
UnstorageAdapterOptionsβ
This is the interface of the Unstorage adapter options.
Propertiesβ
accountByUserIdPrefixβ
accountByUserIdPrefix?: string;
The prefix for the accountByUserId
key
accountKeyPrefixβ
accountKeyPrefix?: string;
The prefix for the account
key
baseKeyPrefixβ
baseKeyPrefix?: string;
The base prefix for your keys
emailKeyPrefixβ
emailKeyPrefix?: string;
The prefix for the emailKey
key
sessionByUserIdKeyPrefixβ
sessionByUserIdKeyPrefix?: string;
The prefix for the sessionByUserId
key
sessionKeyPrefixβ
sessionKeyPrefix?: string;
The prefix for the sessionKey
key
useItemRawβ
useItemRaw?: boolean;
Use getItemRaw/setItemRaw
instead of getItem/setItem
.
This is an experimental feature. Please check unjs/unstorage#142 for more information.
userKeyPrefixβ
userKeyPrefix?: string;
The prefix for the user
key
verificationTokenKeyPrefixβ
verificationTokenKeyPrefix?: string;
The prefix for the verificationToken
key