Skip to content
Migrating from NextAuth.js v4? Read our migration guide.
API reference
@auth/surrealdb-adapter

@auth/surrealdb-adapter

Official SurrealDB adapter for Auth.js / NextAuth.js.

Installation

npm install @auth/surrealdb-adapter surrealdb.js

AccountDoc<T>

type AccountDoc<T>: {
  access_token: string;
  expires_at: number;
  id: string;
  provider: string;
  providerAccountId: string;
  refresh_token: string;
  type: Extract<ProviderType, "oauth" | "oidc" | "email" | "webauthn">;
  userId: T;
};

Type parameters

Type parameterValue
Tstring

Type declaration

access_token?

optional access_token: string;

expires_at?

optional expires_at: number;

id

id: string;

provider

provider: string;

providerAccountId

providerAccountId: string;

refresh_token?

optional refresh_token: string;

type

type: Extract<ProviderType, "oauth" | "oidc" | "email" | "webauthn">;

userId

userId: T;

SessionDoc<T>

type SessionDoc<T>: Document & {
  userId: T;
};

Type declaration

userId

userId: T;

Type parameters

Type parameterValue
Tstring

UserDoc

type UserDoc: Document & {
  email: string;
};

Type declaration

email

email: string;

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 = "http://0.0.0.0:8000"
const username = ""
const password = ""
const namespace = ""
const database = ""
 
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 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 = "http://0.0.0.0:8000"
const username = ""
const password = ""
const namespace = ""
const database = ""
 
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 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

Type parameter
T

Parameters

ParameterType
clientPromise<WebSocketStrategy | HTTPStrategy<T>>

Returns

Adapter


toId()

toId(surrealId): string

Parameters

ParameterType
surrealIdstring

Returns

string


toSurrealId()

toSurrealId(id): string

Parameters

ParameterType
idstring

Returns

string

Auth.js © Balázs Orbán and Team - 2024