SurrealDB Adapter for Better Auth

1.0.0 · active · verified Wed Apr 22

This package provides an official adapter for integrating SurrealDB with the Better Auth authentication library. It enables `better-auth` to persist user and session data within a SurrealDB instance, leveraging its capabilities for authentication flows. The current stable version is `1.0.0`, with a `2.0.0-beta` series actively under development, indicating a continuous release cadence. A key differentiator is its compliance with `better-auth`'s `createAdapter` utility, ensuring full compatibility with core features like CLI-based schema generation. It also offers advanced ID generation options, including `sdk.UUIDv4` and `sdk.UUIDv7`, providing flexibility in how records are identified within SurrealDB. The library is designed for modern JavaScript/TypeScript environments, requiring Node.js >=20.0.0 or Bun >=1.2.0, and relies on peer dependencies for `surrealdb` and `better-auth`.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `surreal-better-auth` adapter with a SurrealDB instance and integrate it into `better-auth`, configuring it with UUIDv7 for ID generation.

import { createSurrealDBAdapter } from 'surreal-better-auth';
import { BetterAuth } from 'better-auth';
import { Surreal } from 'surrealdb';

async function initializeAuth() {
  const db = new Surreal('ws://localhost:8000/rpc');
  // Ensure you've signed in or created a scope/database for the adapter
  await db.signin({
    user: process.env.SURREAL_USER ?? 'root',
    pass: process.env.SURREAL_PASS ?? 'root',
  });
  await db.use('test', 'test');

  const adapter = createSurrealDBAdapter(db, {
    idGenerator: 'sdk.UUIDv7', // Use UUIDv7 for better indexing and time-based ordering
    // You can also define custom ID generation logic here
  });

  const betterAuth = new BetterAuth(adapter);

  console.log('BetterAuth initialized with SurrealDB adapter.');
  // Example: You can now use betterAuth to manage users, sessions, etc.
  // const newUser = await betterAuth.createUser({ email: 'test@example.com', password: 'password123' });
  // console.log('Created user:', newUser);

  // Cleanup (optional)
  // await db.close();
}

initializeAuth().catch(console.error);

view raw JSON →