feat: return false from generateId callback to imply database-generated ID (#3723)

* feat: return false from generateId to imply database-generated ID

* chore: add changeset

* chore: update changeset

---------

Co-authored-by: Bereket Engida <86073083+Bekacru@users.noreply.github.com>
This commit is contained in:
Alec Larson
2025-07-31 20:21:34 -04:00
committed by GitHub
parent 67f84d5d94
commit b27221b356
4 changed files with 9 additions and 3 deletions

View File

@@ -0,0 +1,5 @@
---
"better-auth": patch
---
feat: allow returning false from `generateId` callback to imply database-generated ID

View File

@@ -205,7 +205,7 @@ export type AuthContext = {
generateId: (options: {
model: LiteralUnion<Models, string>;
size?: number;
}) => string;
}) => string | false;
secondaryStorage: SecondaryStorage | undefined;
password: {
hash: (password: string) => Promise<string>;

View File

@@ -16,6 +16,7 @@ import { parseSetCookieHeader, setSessionCookie } from "../../cookies";
import { getOrigin } from "../../utils/url";
import { mergeSchema } from "../../db/schema";
import type { EndpointContext } from "better-call";
import { generateId } from "../../utils/id";
export interface UserWithAnonymous extends User {
isAnonymous: boolean;
@@ -119,7 +120,7 @@ export const anonymous = (options?: AnonymousOptions) => {
async (ctx) => {
const { emailDomainName = getOrigin(ctx.context.baseURL) } =
options || {};
const id = ctx.context.generateId({ model: "user" });
const id = generateId();
const email = `temp-${id}@${emailDomainName}`;
const name = (await options?.generateName?.(ctx)) || "Anonymous";
const newUser = await ctx.context.internalAdapter.createUser(

View File

@@ -762,7 +762,7 @@ export type BetterAuthOptions = {
| ((options: {
model: LiteralUnion<Models, string>;
size?: number;
}) => string)
}) => string | false)
| false;
};
/**