SurrealDB Adapter for Better Auth

0.0.18 · active · verified Wed Apr 22

The `surrealdb-better-auth` package provides a specialized adapter to seamlessly integrate `SurrealDB` as the data persistence layer for the `Better Auth` authentication library. It enables developers to leverage SurrealDB's flexible, real-time database capabilities for storing user accounts, sessions, and other authentication-related data. As of version 0.0.18, this package is in its early stages of development, indicating a pre-1.0 API stability and a likelihood of frequent updates and potential breaking changes. Its key differentiators include secure authentication, real-time data synchronization with SurrealDB, and a focus on high performance and scalability, all while offering easy configuration within a TypeScript-first environment. It's designed to work with specific peer dependency versions of `better-auth` and `surrealdb`.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates the basic setup for integrating SurrealDB with Better Auth, configuring the SurrealDB adapter with server address and credentials, using environment variables for sensitive data.

import { betterAuth } from "better-auth"; // Peer dependency, essential for usage
import { surrealAdapter } from "surrealdb-better-auth";

// Basic configuration for Better Auth with the SurrealDB adapter
export const auth = betterAuth({
	secret: process.env.AUTH_SECRET ?? 'super-secret-key-please-change', // A strong secret is crucial for production
	// ... other Better Auth options as needed
	database: surrealAdapter({
		address: process.env.SURREALDB_ADDRESS ?? "http://localhost:8000", // Your SurrealDB server address
		username: process.env.SURREALDB_USERNAME ?? "root", // Your SurrealDB username
		password: process.env.SURREALDB_PASSWORD ?? "root", // Your SurrealDB password
		ns: process.env.SURREALDB_NAMESPACE ?? "namespace", // Your namespace
		db: process.env.SURREALDB_DATABASE ?? "database" // Your database name
	})
});

// Example usage (simplified, assuming 'auth' is exported and used elsewhere)
async function authenticateUser(email: string, pass: string) {
    // In a real application, you'd call 'auth.authenticateUser' or similar methods.
    console.log(`Attempting to authenticate user: ${email}`);
    // This quickstart only shows the setup of the adapter, not full auth flow.
    // For full flow, refer to Better Auth documentation.
    // Example: const user = await auth.authenticate('credentials', { email, password: pass });
    // console.log("User authenticated:", user);
    console.log("SurrealDB adapter initialized for Better Auth.");
    return true; // Placeholder for successful initialization
}

// Call a dummy function to make the quickstart runnable and demonstrate setup
authenticateUser("test@example.com", "password123");

view raw JSON →