@auth/surrealdb-adapter
Official SurrealDB adapter for Auth.js / NextAuth.js.
Installationβ
- npm
- Yarn
- pnpm
npm install @auth/surrealdb-adapter surrealdb.js
yarn add @auth/surrealdb-adapter surrealdb.js
pnpm add @auth/surrealdb-adapter surrealdb.js
SurrealDBAdapter()β
SurrealDBAdapter<
T
>(client
):Adapter
Setupβ
The SurrealDB adapter does not handle connections automatically, so you will have to make sure that you pass the Adapter a SurrealDBClient
that is connected already. Below you can see an example how to do this.
Add the SurrealDB clientβ
Option 1/2 β Using RPC:β
import { Surreal } from "surrealdb.js";
const connectionString = ... // i.e. "http://0.0.0.0:8000"
const user = ...
const pass = ...
const ns = ...
const db = ...
const clientPromise = new Promise<Surreal>(async (resolve, reject) => {
const db = new Surreal();
try {
await db.connect(`${connectionString}/rpc`, {
ns, db, auth: { user, pass }
})
resolve(db)
} catch (e) {
reject(e)
}
})
// Export a module-scoped MongoClient promise. By doing this in a
// separate module, the client can be shared across functions.
export default clientPromise
Option 2/2 β Using HTTP:β
Usefull in serverlees environments like Vercel.
import { ExperimentalSurrealHTTP } from "surrealdb.js"
const connectionString = ... // i.e. "http://0.0.0.0:8000"
const user = ...
const pass = ...
const ns = ...
const db = ...
const clientPromise = new Promise<ExperimentalSurrealHTTP<typeof fetch>>(async (resolve, reject) => {
try {
const db = new ExperimentalSurrealHTTP(connectionString, {
fetch,
ns, db, auth: { user, pass }
})
resolve(db)
} catch (e) {
reject(e)
}
})
// Export a module-scoped MongoClient promise. By doing this in a
// separate module, the client can be shared across functions.
export default clientPromise
Configure Auth.jsβ
import NextAuth from "next-auth"
import { SurrealDBAdapter } from "@auth/surrealdb-adapter"
import clientPromise from "../../../lib/surrealdb"
// For more information on each option (and a full list of options) go to
// https://authjs.dev/reference/providers/oauth
export default NextAuth({
adapter: SurrealDBAdapter(clientPromise),
...
})
Type parametersβ
βͺ T
Parametersβ
βͺ client: Promise
<WebSocketStrategy
| HTTPStrategy
<T
>>
Returnsβ
Adapter