Better Auth InstantDB Adapter
The Better Auth InstantDB Adapter provides a seamless integration layer, allowing developers to utilize InstantDB as the backing database for Better Auth authentication services. This package, currently at version 1.3.2, facilitates complete authentication flows, automatic session synchronization between Better Auth and InstantDB, and offers type-safe development with TypeScript. While no explicit release cadence is stated, the presence of recent updates (e.g., v1.1.0 addressing email synchronization) suggests active maintenance. Its key differentiators include simplifying the complex task of managing user authentication data by offloading it to InstantDB, providing a customizable setup, and ensuring session consistency across client and server environments. It requires both `@instantdb/admin` for server-side operations and `better-auth` as core peer dependencies.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'appId')
cause The InstantDB client (admin or react) was initialized without providing the `appId` or it was undefined due to a missing environment variable.fixCheck that `process.env.VITE_INSTANT_APP_ID` (for server) or `process.env.NEXT_PUBLIC_INSTANT_APP_ID` (for client) are correctly set and accessible in your environment configuration. -
InstantDB: Permission denied for operation 'create' on entity 'users'
cause The InstantDB permissions (in `instant.perms.ts`) do not allow the necessary operations (e.g., 'create', 'update', 'view') for the `users` or other authentication entities.fixReview and update your `instant.perms.ts` file. Ensure that roles for `users`, `accounts`, and `sessions` are correctly defined, particularly `bind` and `allow` rules, to permit Better Auth's operations. -
Error: `db` is required in instantAdapter options.
cause The `instantAdapter` was initialized in `betterAuth` without providing an initialized InstantDB admin client.fixEnsure `adminDb` (or your variable for the InstantDB admin client) is correctly initialized using `init({ schema, appId, adminToken, ... })` from `@instantdb/admin` and passed to `instantAdapter({ db: adminDb, ... })`.
Warnings
- gotcha Email synchronization was updated in v1.1.0 to properly sync between 'users' and '$users' tables. While new sign-ins will auto-sync, existing users' emails in the '$users' table will only update on their next sign-in. To batch update historical data, you need to manually run the provided `sync-emails.ts` script.
- breaking InstantDB schema and permissions are critical for security and functionality. The Better Auth CLI can generate a schema, but permissions must be manually configured in `instant.perms.ts`. Failure to correctly define permissions (e.g., for `users`, `accounts`, `sessions`) can lead to security vulnerabilities or prevent authentication flows from working.
- gotcha Incorrect or missing environment variables (e.g., `VITE_INSTANT_APP_ID`, `INSTANT_ADMIN_TOKEN`, `NEXT_PUBLIC_INSTANT_APP_ID`) will prevent both the server-side InstantDB admin client and the client-side `@instantdb/react` client from initializing correctly, leading to authentication failures.
Install
-
npm install better-auth-instantdb -
yarn add better-auth-instantdb -
pnpm add better-auth-instantdb
Imports
- instantAdapter
const { instantAdapter } = require('better-auth-instantdb')import { instantAdapter } from 'better-auth-instantdb' - useInstantAuth
import useInstantAuth from 'better-auth-instantdb'
import { useInstantAuth } from 'better-auth-instantdb' - init
import { init } from '@instantdb/admin'
Quickstart
import { betterAuth } from "better-auth"
import { instantAdapter } from "better-auth-instantdb"
import { init } from "@instantdb/admin"
// Mock schema and environment variables for demonstration
const schema = {}; // In a real app, this would be your InstantDB schema
process.env.VITE_INSTANT_APP_ID = process.env.VITE_INSTANT_APP_ID ?? 'your-app-id';
process.env.INSTANT_ADMIN_TOKEN = process.env.INSTANT_ADMIN_TOKEN ?? 'your-admin-token';
// Create InstantDB admin client
export const adminDb = init({
schema,
appId: process.env.VITE_INSTANT_APP_ID,
adminToken: process.env.INSTANT_ADMIN_TOKEN,
useDateObjects: true
})
// Create Better Auth instance with InstantDB adapter
export const auth = betterAuth({
database: instantAdapter({
db: adminDb,
usePlural: true, // Optional: set to true if your schema uses plural table names
debugLogs: false // Optional: set to true to see detailed logs
}),
emailAndPassword: {
enabled: true
},
// ... other Better Auth configuration options
});
console.log('Better Auth with InstantDB adapter initialized.');