SurrealDB Adapter
Resources
Setup
Installation
npm install @auth/surrealdb-adapter surrealdb.js
Environment Variables
AUTH_SURREALDB_CONNECTION
AUTH_SURREALDB_USERNAME
AUTH_SURREALDB_PASSWORD
AUTH_SURREALDB_NS
AUTH_SURREALDB_DB
Configuration
./auth.ts
import NextAuth from "next-auth"
import { SurrealDBAdapter } from "@auth/surrealdb-adapter"
import clientPromise from "./lib/surrealdb"
export const { handlers, auth, signIn, signOut } = NextAuth({
providers: [],
adapter: SurrealDBAdapter(clientPromise),
})
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.
Authorization
Option 1 – Using RPC:
./lib/surrealdb.ts
import { Surreal } from "surrealdb.js"
const connectionString = process.env.AUTH_SURREALDB_CONNECTION
const user = process.env.AUTH_SURREALDB_USERNAME
const pass = process.env.AUTH_SURREALDB_PASSWORD
const ns = process.env.AUTH_SURREALDB_NS
const db = process.env.AUTH_SURREALDB_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 – Using HTTP:
Useful in serverless environments like Vercel.
./lib/surrealdb.ts
import { ExperimentalSurrealHTTP } from "surrealdb.js"
const connectionString = process.env.AUTH_SURREALDB_CONNECTION
const user = process.env.AUTH_SURREALDB_USERNAME
const pass = process.env.AUTH_SURREALDB_PASSWORD
const ns = process.env.AUTH_SURREALDB_NS
const db = process.env.AUTH_SURREALDB_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