mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-06 04:19:20 +00:00
refactor: move user schema to core
This commit is contained in:
@@ -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[] }[] = [];
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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>;
|
||||
|
||||
@@ -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,
|
||||
|
||||
7
packages/core/src/db/schema/shared.ts
Normal file
7
packages/core/src/db/schema/shared.ts
Normal 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()),
|
||||
});
|
||||
16
packages/core/src/db/schema/user.ts
Normal file
16
packages/core/src/db/schema/user.ts
Normal 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>;
|
||||
Reference in New Issue
Block a user