SurrealDB Adapter for Better Auth
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
-
TypeError: surrealAdapter is not a function
cause Attempting to import `surrealAdapter` using CommonJS `require()` syntax or an incorrect named/default import.fixUse `import { surrealAdapter } from 'surrealdb-better-auth';` in an ESM-enabled project. -
Cannot connect to SurrealDB at http://localhost:8000 (or similar address)
cause Incorrect SurrealDB connection parameters (address, username, password, namespace, database) or the SurrealDB instance is not running/accessible.fixVerify that your SurrealDB server is running and accessible at the specified address, and double-check all connection credentials in your `surrealAdapter` configuration. -
Error: Peer dependency 'better-auth@^1.2.7 || ^1.4.0' not installed or mismatched.
cause The `better-auth` package, a required peer dependency, is either missing from your project or its installed version does not match the range expected by `surrealdb-better-auth`.fixInstall `better-auth` with a compatible version, e.g., `npm install better-auth@1.4.0` or `pnpm add better-auth@1.4.0`. -
Error: The 'database' option must be an instance of an Adapter.
cause The `surrealAdapter()` function was not correctly called or its return value was not properly passed to the `database` option of `betterAuth()`.fixEnsure `betterAuth` is configured as `database: surrealAdapter({...})` and that `surrealAdapter` is called with its required options.
Warnings
- breaking As a pre-1.0 release (version 0.0.18), the API of `surrealdb-better-auth` is not yet stable. Future minor or patch versions may introduce breaking changes without a major version bump, requiring adjustments to your code.
- breaking This package has strict peer dependency requirements for `better-auth` (`^1.2.7 || ^1.4.0`) and `surrealdb` (`1.3.2`). Using incompatible versions of these dependencies can lead to runtime errors or unexpected behavior.
- gotcha The default SurrealDB schemas defined by this adapter (Account, Session, User) are `SCHEMALESS`. This provides flexibility but means SurrealDB itself will not enforce data types or structure, leaving validation entirely to the application layer. Inconsistent data might arise if not handled carefully.
- breaking The package requires Node.js version 18 or higher (or Bun). Running on older Node.js versions may result in unsupported syntax errors or unexpected failures.
Install
-
npm install surrealdb-better-auth -
yarn add surrealdb-better-auth -
pnpm add surrealdb-better-auth
Imports
- surrealAdapter
const surrealAdapter = require('surrealdb-better-auth');import { surrealAdapter } from 'surrealdb-better-auth'; - betterAuth
import betterAuth from 'better-auth';
import { betterAuth } from 'better-auth'; - Adapter
import { Adapter } from 'better-auth';import type { Adapter } from 'better-auth';
Quickstart
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");