@auth/fauna-adapter
Official Fauna adapter for Auth.js / NextAuth.js.
Installationβ
- npm
- Yarn
- pnpm
npm install @auth/fauna-adapter fauna
yarn add @auth/fauna-adapter fauna
pnpm add @auth/fauna-adapter fauna
FaunaAdapter()β
FaunaAdapter(client, config?): Adapter
Setupβ
This is the Fauna Adapter for Auth.js. This package can only be used in conjunction with the primary next-auth
and other framework packages. It is not a standalone package.
You can find the Fauna schema and seed information in the docs at authjs.dev/reference/adapter/fauna.
Configure Auth.jsβ
import NextAuth from "next-auth"
import { Client } from "fauna"
import { FaunaAdapter } from "@auth/fauna-adapter"
const client = new Client({
secret: "secret",
endpoint: new URL('http://localhost:8443')
})
// For more information on each option (and a full list of options) go to
// https://authjs.dev/reference/configuration/auth-options
export default NextAuth({
// https://authjs.dev/reference/providers/
providers: [],
adapter: FaunaAdapter(client)
...
})
Schemaβ
Run the following FQL code inside the Shell
tab in the Fauna dashboard to set up the appropriate collections and indexes.
Collection.create({
name: "Account",
indexes: {
byUserId: {
terms: [
{ field: "userId" }
]
},
byProviderAndProviderAccountId: {
terms [
{ field: "provider" },
{ field: "providerAccountId" }
]
},
}
})
Collection.create({
name: "Session",
constraints: [
{
unique: ["sessionToken"],
status: "active",
}
],
indexes: {
bySessionToken: {
terms: [
{ field: "sessionToken" }
]
},
byUserId: {
terms [
{ field: "userId" }
]
},
}
})
Collection.create({
name: "User",
constraints: [
{
unique: ["email"],
status: "active",
}
],
indexes: {
byEmail: {
terms [
{ field: "email" }
]
},
}
})
Collection.create({
name: "VerificationToken",
indexes: {
byIdentifierAndToken: {
terms [
{ field: "identifier" },
{ field: "token" }
]
},
}
})
This schema is adapted for use in Fauna and based upon our main schema
Custom collection namesβ
If you want to use custom collection names, you can pass them as an option to the adapter, like this:
FaunaAdapter(client, {
collectionNames: {
user: "CustomUser",
account: "CustomAccount",
session: "CustomSession",
verificationToken: "CustomVerificationToken",
}
})
Make sure the collection names you pass to the provider match the collection names of your Fauna database.
Migrating from v1β
In v2, we've renamed the collections to use uppercase naming, in accordance with Fauna best practices. If you're migrating from v1, you'll need to rename your collections to match the new naming scheme. Additionally, we've renamed the indexes to match the new method-like index names.
Migration scriptβ
Run this FQL script inside a Fauna shell for the database you're migrating from v1 to v2 (it will rename your collections and indexes to match):
Collection.byName("accounts")!.update({
name: "Account"
indexes: {
byUserId: {
terms: [{ field: "userId" }]
},
byProviderAndProviderAccountId: {
terms: [{ field: "provider" }, { field: "providerAccountId" }]
},
account_by_provider_and_provider_account_id: null,
accounts_by_user_id: null
}
})
Collection.byName("sessions")!.update({
name: "Session",
indexes: {
bySessionToken: {
terms: [{ field: "sessionToken" }]
},
byUserId: {
terms: [{ field: "userId" }]
},
session_by_session_token: null,
sessions_by_user_id: null
}
})
Collection.byName("users")!.update({
name: "User",
indexes: {
byEmail: {
terms: [{ field: "email" }]
},
user_by_email: null
}
})
Collection.byName("verification_tokens")!.update({
name: "VerificationToken",
indexes: {
byIdentifierAndToken: {
terms: [{ field: "identifier" }, { field: "token" }]
},
verification_token_by_identifier_and_token: null
}
})
Parametersβ
βͺ client: Client
βͺ config?: AdapterConfig
Returnsβ
Adapter