@auth/pouchdb-adapter
Official PouchDB adapter for Auth.js / NextAuth.js.
Installation
npm install pouchdb pouchdb-find @auth/pouchdb-adapter
PouchDBAdapterOptions
Configure the adapter
Properties
indexes?
optional indexes: IndexConfig;
Override the default index names.
Default
{
userByEmail: "nextAuthUserByEmail",
accountByProviderId: "nextAuthAccountByProviderId",
sessionByToken: "nextAuthSessionByToken",
verificationTokenByToken: "nextAuthVerificationRequestByToken"
}
pouchdb
pouchdb: Database<{}>;
Your PouchDB instance, with the pouchdb-find
plugin installed.
Example
import PouchDB from "pouchdb"
PouchDB
.plugin(require("pouchdb-adapter-leveldb")) // Or any other adapter
.plugin(require("pouchdb-find")) // Don't forget the `pouchdb-find` plugin
const pouchdb = new PouchDB("auth_db", \{ adapter: "leveldb" \})
#### prefixes?
```ts
optional prefixes: PrefixConfig;
Override the default prefix names.
Default
{
user: "USER",
account: "ACCOUNT",
session: "SESSION",
verificationToken: "VERIFICATION-TOKEN"
}
PouchDBAdapter()
PouchDBAdapter(options): Adapter
Depending on your architecture you can use PouchDB’s http adapter to reach any database compliant with the CouchDB protocol (CouchDB, Cloudant, etc.) or use any other PouchDB compatible adapter (leveldb, in-memory, etc.)
Setup
Your PouchDB instance MUST provide the pouchdb-find
plugin since it is used internally by the adapter to build and manage indexes
Add this adapter to your pages/api/auth/[...nextauth].js
next-auth configuration object:
import NextAuth from "next-auth"
import GoogleProvider from "next-auth/providers/google"
import { PouchDBAdapter } from "@auth/pouchdb-adapter"
import PouchDB from "pouchdb"
// Setup your PouchDB instance and database
PouchDB
.plugin(require("pouchdb-adapter-leveldb")) // Or any other adapter
.plugin(require("pouchdb-find")) // Don't forget the `pouchdb-find` plugin
const pouchdb = new PouchDB("auth_db", { adapter: "leveldb" })
// For more information on each option (and a full list of options) go to
// https://authjs.dev/reference/core/types#authconfig
export default NextAuth({
// https://authjs.dev/getting-started/authentication/oauth
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
}),
],
adapter: PouchDBAdapter(pouchdb),
})
Advanced usage
Memory-First Caching Strategy
If you need to boost your authentication layer performance, you may use PouchDB’s powerful sync features and various adapters, to build a memory-first caching strategy.
Use an in-memory PouchDB as your main authentication database, and synchronize it with any other persisted PouchDB. You may do a one way, one-off replication at startup from the persisted PouchDB into the in-memory PouchDB, then two-way, continuous sync.
This will most likely not increase performance much in a serverless environment due to various reasons such as concurrency, function startup time increases, etc.
For more details, please see https://pouchdb.com/api.html#sync
Parameters
Parameter | Type |
---|---|
options | PouchDBAdapterOptions |
Returns
createIndexes()
createIndexes(pouchdb, indexes?): Promise<void>
Parameters
Parameter | Type |
---|---|
pouchdb | Database <{}> |
indexes ? | IndexConfig |
Returns
Promise
<void
>