Skip to Content
Better Auth

Better Auth

@cyphra/better-auth is a Better Auth  database adapter built on the Cyphra ORM. Users, sessions, accounts, verifications and plugin models are nodes in the same graph as your own models. They are described in the same schema.cyphra, migrated by cyphra migrate, and queried through the same typed client.

Install

npm i @cyphra/better-auth better-auth cyphra npm i -D @cyphra/cli

The adapter needs Neo4j 5.6+ and better-auth 1.7.27 or later. If the project has no Cyphra setup yet, run npx cyphra init first (it creates cyphra.json, schema.cyphra and migrations/).

Configure Better Auth

Pass your generated Cyphra client as db. The adapter reuses its connection (db.$client), so auth and app queries share one driver.

db.ts
import { createClient } from "./cyphra.gen"; export const db = createClient({ uri: process.env.NEO4J_URI!, user: process.env.NEO4J_USERNAME!, password: process.env.NEO4J_PASSWORD!, });
auth.ts
import { betterAuth } from "better-auth"; import { cyphraAdapter } from "@cyphra/better-auth"; import { db } from "./db"; export const auth = betterAuth({ database: cyphraAdapter({ db }), emailAndPassword: { enabled: true }, });

Before the first cyphra generate there is no cyphra.gen.ts. Pass a CyphraClient instead:

auth.ts
import { CyphraClient } from "cyphra"; const client = new CyphraClient({ uri: process.env.NEO4J_URI!, user: process.env.NEO4J_USERNAME!, password: process.env.NEO4J_PASSWORD!, }); export const auth = betterAuth({ database: cyphraAdapter({ client }) });
OptionDescription
dbYour generated client. Its connection is reused.
clientA CyphraClient, if you do not pass db.
usePluralPlural model names (users, sessions, …), so labels Users, Sessions. Default false.
numberFieldsnumber fields stored as Float instead of Int. See Fractional numbers.
debugLogsBetter Auth’s adapter debug logs. Logs can contain tokens: keep them off in production.

The adapter builds its Cyphra schema from your Better Auth configuration when Better Auth starts, so it always matches the plugins you enable. It does not read schema.cyphra at runtime.

Generate the schema

Write the Better Auth models into schema.cyphra

npx @better-auth/cli generate

The adapter adds the Better Auth nodes to schema.cyphra (or to the schema path in cyphra.json, or to --output). If the file exists, it is updated in place: Better Auth fields are added or rewritten, and everything else is kept as you wrote it, comments included. That covers your own models and relationships, and fields you added to auth nodes. Run the command again after adding a plugin.

Create and apply a migration

npx cyphra migrate dev --name better-auth

This creates the constraints and indexes for the auth nodes (User.id, User.email, Session.token, …) together with those of your own models.

Regenerate the typed client

npx cyphra generate

For the default configuration, the generated nodes look like this (excerpt):

schema.cyphra
node User { id String @id @default(cuid()) name String email String @unique emailVerified Boolean image String? createdAt DateTime updatedAt DateTime sessions Session[] @relationship(type: "HAS_SESSION", direction: OUT, onDelete: Cascade) accounts Account[] @relationship(type: "HAS_ACCOUNT", direction: OUT, onDelete: Cascade) } node Session { id String @id @default(cuid()) expiresAt DateTime token String @unique createdAt DateTime updatedAt DateTime ipAddress String? userAgent String? user User @relationship(type: "HAS_SESSION", direction: IN) }

How auth data is stored

  • Labels are the database model names in PascalCase: user → User, twoFactor → TwoFactor, a custom modelName: "app_user" → AppUser.
  • Foreign keys are edges. A Better Auth field with references, such as Session.userId, is not stored as a property. It is the edge (User)-[:HAS_SESSION]->(Session), with a to-one field on the child (user, from userId without the Id suffix) and a list field on the parent (sessions). Better Auth still reads and writes userId; the adapter translates it to the edge. A model with two foreign keys to the same parent gets one edge type per key (HAS_INVITATION_INVITER).
  • Types: string → String, number → Int (or Float with numberFields), boolean → Boolean, date → DateTime, string[] / number[] → lists, json → String (JSON text). required: false makes the field optional; unique → @unique; index → @index.
  • Sorting by a foreign key (sortBy: { field: "userId" }) orders by the referenced field of the related node, through the edge. Nodes without the edge (an optional foreign key that is not set) sort as null: last ascending, first descending.
  • Deletes follow references.onDelete, written into the schema as onDelete rules on the parent’s list field and applied by the Cyphra client in the same transaction: cascade (Better Auth’s default) becomes onDelete: Cascade and deletes the children, restrict and no action become onDelete: Restrict and refuse the delete while children exist, and set null / set default keep the default (SetNull): the edge is removed. Deleting a user never deletes your own nodes unless your own fields say so; they lose their edge to it. Your app’s db.user.delete(…) applies the same rules once schema.cyphra is regenerated (npx @better-auth/cli generate, then cyphra generate).
  • Single-use tokens: consumeOne locks the node before deleting it, so of several concurrent requests for the same verification token exactly one gets it. incrementOne is race-safe the same way.
  • Joins: with Better Auth’s advanced.database.joins, joined models are loaded through the edges in the same query.

Fractional numbers

Better Auth’s number type does not say whether a field holds integers or fractions, so every number field is an Int by default and a fractional value is rejected with a validation error. Better Auth’s own models and plugins store counters and timestamps-as-numbers, which are integers. For a field of your own (or of a plugin) that holds fractions, list it in numberFields, by model and field. Schema keys and database names both work:

auth.ts
const numberFields = { wallet: { balance: "float" } } as const; export const auth = betterAuth({ database: cyphraAdapter({ db, numberFields }), plugins: [walletPlugin()], });

npx @better-auth/cli generate then writes balance Float in schema.cyphra. Pass the same numberFields to authSchema(auth.options, { numberFields }) and neo4jBetterAuthMigration if you use them. An entry that names an unknown model or field, or a field that is not a number / number[], fails when Better Auth starts.

Switching an existing field from int to float needs no data migration: Neo4j compares and sorts integers and floats as numbers, so stored integers still match filters and read back unchanged, and new writes store floats. cyphra migrate dev records the change and warns that existing values are not converted, which is safe here.

Relate your models to users

Add relations on User and on your models in schema.cyphra. npx @better-auth/cli generate keeps them.

schema.cyphra
node User { id String @id @default(cuid()) email String @unique // … the other Better Auth fields projects Project[] @relationship(type: "OWNS", direction: OUT) } node Project { id String @id @default(uuid()) title String owner User @relationship(type: "OWNS", direction: IN) }

After cyphra migrate dev and cyphra generate, auth nodes and your nodes are one typed graph:

const session = await auth.api.getSession({ headers }); if (!session) throw new Error("Unauthorized"); await db.project.create({ data: { title: "Analytical Engine", owner: { connect: { id: session.user.id } } }, }); const me = await db.user.findUniqueOrThrow({ where: { id: session.user.id }, include: { projects: true, sessions: { select: { expiresAt: true } } }, }); // me.projects: { id: string; title: string }[] // me.sessions: { expiresAt: Date }[] // The projects of whoever holds this session token, in one query: const projects = await db.project.findMany({ where: { owner: { is: { sessions: { some: { token } } } } }, });

Migrating from neo4j-better-auth

@cyphra/better-auth replaces the community adapter neo4j-better-auth. The storage layout changes, so existing data has to be converted once:

neo4j-better-auth@cyphra/better-auth
Labels are model names: user, sessionPascalCase: User, Session
Foreign keys are a property and a HAS_* edgeThe HAS_* edge only
Dates are ISO 8601 stringsDateTime values
Numbers are floatsIntegers

Edge types and directions stay the same for single-word model names (HAS_SESSION, HAS_ACCOUNT). Model names with several words now get underscores (twoFactor: HAS_TWOFACTOR → HAS_TWO_FACTOR); the script below renames those edges.

Back up the database and stop the app

The script rewrites every auth node.

Run the migration Cypher

For the default Better Auth configuration (no plugins), the script is:

migrate-better-auth.cypher
// 1. Drop the constraints and indexes neo4j-better-auth created on the old labels. DROP CONSTRAINT `user_id_unique` IF EXISTS; DROP CONSTRAINT `user_email_unique` IF EXISTS; DROP CONSTRAINT `session_id_unique` IF EXISTS; DROP CONSTRAINT `session_token_unique` IF EXISTS; DROP INDEX `session_userId_idx` IF EXISTS; DROP CONSTRAINT `account_id_unique` IF EXISTS; DROP INDEX `account_userId_idx` IF EXISTS; DROP CONSTRAINT `verification_id_unique` IF EXISTS; DROP INDEX `verification_identifier_idx` IF EXISTS; // 2. Lowercase labels → PascalCase. MATCH (n:`user`) SET n:`User` REMOVE n:`user`; MATCH (n:`session`) SET n:`Session` REMOVE n:`session`; MATCH (n:`account`) SET n:`Account` REMOVE n:`account`; MATCH (n:`verification`) SET n:`Verification` REMOVE n:`verification`; // 3. Foreign-key properties → HAS_* edges, then drop the properties. MATCH (c:`Session`) WHERE c.`userId` IS NOT NULL MATCH (p:`User`) WHERE p.`id` = c.`userId` MERGE (p)-[:`HAS_SESSION`]->(c); MATCH (c:`Account`) WHERE c.`userId` IS NOT NULL MATCH (p:`User`) WHERE p.`id` = c.`userId` MERGE (p)-[:`HAS_ACCOUNT`]->(c); MATCH (c:`Session`) WHERE c.`userId` IS NOT NULL REMOVE c.`userId`; MATCH (c:`Account`) WHERE c.`userId` IS NOT NULL REMOVE c.`userId`; // 4. ISO date strings → datetime() (toString(x) = x only holds for strings). MATCH (n:`User`) WHERE toString(n.`createdAt`) = n.`createdAt` SET n.`createdAt` = datetime(n.`createdAt`); MATCH (n:`User`) WHERE toString(n.`updatedAt`) = n.`updatedAt` SET n.`updatedAt` = datetime(n.`updatedAt`); MATCH (n:`Session`) WHERE toString(n.`expiresAt`) = n.`expiresAt` SET n.`expiresAt` = datetime(n.`expiresAt`); MATCH (n:`Session`) WHERE toString(n.`createdAt`) = n.`createdAt` SET n.`createdAt` = datetime(n.`createdAt`); MATCH (n:`Session`) WHERE toString(n.`updatedAt`) = n.`updatedAt` SET n.`updatedAt` = datetime(n.`updatedAt`); MATCH (n:`Account`) WHERE toString(n.`accessTokenExpiresAt`) = n.`accessTokenExpiresAt` SET n.`accessTokenExpiresAt` = datetime(n.`accessTokenExpiresAt`); MATCH (n:`Account`) WHERE toString(n.`refreshTokenExpiresAt`) = n.`refreshTokenExpiresAt` SET n.`refreshTokenExpiresAt` = datetime(n.`refreshTokenExpiresAt`); MATCH (n:`Account`) WHERE toString(n.`createdAt`) = n.`createdAt` SET n.`createdAt` = datetime(n.`createdAt`); MATCH (n:`Account`) WHERE toString(n.`updatedAt`) = n.`updatedAt` SET n.`updatedAt` = datetime(n.`updatedAt`); MATCH (n:`Verification`) WHERE toString(n.`expiresAt`) = n.`expiresAt` SET n.`expiresAt` = datetime(n.`expiresAt`); MATCH (n:`Verification`) WHERE toString(n.`createdAt`) = n.`createdAt` SET n.`createdAt` = datetime(n.`createdAt`); MATCH (n:`Verification`) WHERE toString(n.`updatedAt`) = n.`updatedAt` SET n.`updatedAt` = datetime(n.`updatedAt`);
cypher-shell -a "$NEO4J_URI" -u "$NEO4J_USERNAME" -p "$NEO4J_PASSWORD" -f migrate-better-auth.cypher

Every statement can be run again safely. With plugins, custom model or field names, or usePlural, print the script for your own configuration instead. It also covers plugin models, renamed edge types and number fields (floats → toInteger()):

print-migration.ts
import { neo4jBetterAuthMigration } from "@cyphra/better-auth"; import { auth } from "./auth"; console.log( neo4jBetterAuthMigration(auth.options) .map((s) => `${s};`) .join("\n"), );

Create the new constraints

Generate the schema and migrate as in Generate the schema, or on a production database apply the committed migrations:

npx cyphra migrate deploy

Switch the adapter

Replace neo4jAdapter({ driver }) with cyphraAdapter({ db }) (or { client }) in auth.ts.

On a large graph, run each statement in batches, in an auto-commit transaction (:auto in Neo4j Browser), for example MATCH (n:session) CALL { WITH n SET n:Session REMOVE n:session } IN TRANSACTIONS OF 10000 ROWS.

Limitations

  • Better Auth’s table-level indexes (declared by plugins) are created: a compound index becomes @@unique([…]) or @@index([…]) on the node (keeping its name when it sets one), a one-field index @unique or @index on the field. An index that includes a foreign key (userId, stored as an edge, not a property) cannot be a property index and is left out; the edge is already how Cyphra finds the related nodes.
  • number fields are integers unless listed in numberFields; a fractional value in an Int field is rejected with a validation error.
  • mode: "insensitive" filters (used for emails) compare with toLower() and do not use the property index.

See also

  • Schema: the .cyphra language and relations.
  • Typed client: queries, include and nested writes on auth nodes and your own.
  • Migrations: cyphra migrate dev and deploy.
Last updated on