refactor: move user schema to core

This commit is contained in:
Alex Yang
2025-10-02 11:42:45 -07:00
parent 246a2e8b7a
commit a6c000d428
6 changed files with 29 additions and 17 deletions

View File

@@ -16,7 +16,6 @@ import type {
} from "./types";
import { colors } from "../../utils/colors";
import type { DBFieldAttribute } from "@better-auth/core/db";
import { parseUserInput } from "../../db";
export * from "./types";
let debugLogs: { instance: string; args: any[] }[] = [];

View File

@@ -3,13 +3,7 @@ import type { AuthPluginSchema } from "../types/plugins";
import type { BetterAuthOptions } from "../types/options";
import { APIError } from "better-call";
import type { Account, Session, User } from "../types";
import type { DBFieldAttribute } from "@better-auth/core/db";
export const coreSchema = z.object({
id: z.string(),
createdAt: z.date().default(() => new Date()),
updatedAt: z.date().default(() => new Date()),
});
import { type DBFieldAttribute, coreSchema } from "@better-auth/core/db";
export const accountSchema = coreSchema.extend({
providerId: z.string(),
@@ -36,13 +30,6 @@ export const accountSchema = coreSchema.extend({
password: z.string().nullish(),
});
export const userSchema = coreSchema.extend({
email: z.string().transform((val) => val.toLowerCase()),
emailVerified: z.boolean().default(false),
name: z.string(),
image: z.string().nullish(),
});
export const sessionSchema = coreSchema.extend({
userId: z.coerce.string(),
expiresAt: z.date(),

View File

@@ -2,7 +2,6 @@ import type { BetterAuthOptions } from "./options";
import type {
accountSchema,
sessionSchema,
userSchema,
verificationSchema,
} from "../db/schema";
import type { Auth } from "../auth";
@@ -10,6 +9,7 @@ import type { InferFieldsFromOptions, InferFieldsFromPlugins } from "../db";
import type { StripEmptyObjects, UnionToIntersection } from "./helper";
import type { BetterAuthPlugin } from "./plugins";
import type * as z from "zod";
import type { User } from "@better-auth/core/db";
export type Models =
| "user"
@@ -89,7 +89,7 @@ interface RateLimit {
lastRequest: number;
}
export type User = z.infer<typeof userSchema>;
export type { User } from "@better-auth/core/db";
export type Account = z.infer<typeof accountSchema>;
export type Session = z.infer<typeof sessionSchema>;
export type Verification = z.infer<typeof verificationSchema>;

View File

@@ -6,6 +6,9 @@ import type {
BetterAuthDBSchema,
} from "./type";
export { coreSchema } from "./schema/shared";
export { userSchema, type User } from "./schema/user";
export type {
DBFieldAttribute,
DBFieldAttributeConfig,

View File

@@ -0,0 +1,7 @@
import * as z from "zod";
export const coreSchema = z.object({
id: z.string(),
createdAt: z.date().default(() => new Date()),
updatedAt: z.date().default(() => new Date()),
});

View File

@@ -0,0 +1,16 @@
import * as z from "zod";
import { coreSchema } from "./shared";
export const userSchema = coreSchema.extend({
email: z.string().transform((val) => val.toLowerCase()),
emailVerified: z.boolean().default(false),
name: z.string(),
image: z.string().nullish(),
});
/**
* User schema type used by better-auth, note that it's possible that user could have additional fields
*
* todo: we should use generics to extend this type with additional fields from plugins and options in the future
*/
export type User = z.infer<typeof userSchema>;