Skip to content
Migrating from NextAuth.js v4? Read our migration guide.

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 username = process.env.AUTH_SURREALDB_USERNAME
const password = process.env.AUTH_SURREALDB_PASSWORD
const namespace = process.env.AUTH_SURREALDB_NAMESPACE
const database = process.env.AUTH_SURREALDB_DATABASE
if (!connectionString || !username || !password || !namespace || !database) {
  throw new Error(
    "SurrealDB connection string, username, password, namespace, and database are required"
  )
}
 
const clientPromise = new Promise<Surreal>(async (resolve, reject) => {
  const db = new Surreal()
  try {
    await db.connect(`${connectionString}/rpc`, {
      namespace,
      database,
      auth: {
        username,
        password,
      },
    })
    resolve(db)
  } catch (e) {
    reject(e)
  }
})
 
// Export a module-scoped Promise<Surreal>. 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 username = process.env.AUTH_SURREALDB_USERNAME
const password = process.env.AUTH_SURREALDB_PASSWORD
const namespace = process.env.AUTH_SURREALDB_NAMESPACE
const database = process.env.AUTH_SURREALDB_DATABASE
if (!connectionString || !username || !password || !namespace || !database) {
  throw new Error(
    "SurrealDB connection string, username, password, namespace, and database are required"
  )
}
 
const clientPromise = new Promise<ExperimentalSurrealHTTP<typeof fetch>>(
  async (resolve, reject) => {
    try {
      const db = new ExperimentalSurrealHTTP(connectionString, {
        fetch,
        namespace,
        database,
        auth: {
          username,
          password,
        },
      })
      resolve(db)
    } catch (e) {
      reject(e)
    }
  }
)
 
// Export a module-scoped Promise<Surreal>. By doing this in a
// separate module, the client can be shared across functions.
export default clientPromise
Auth.js © Balázs Orbán and Team - 2024