mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-09 12:27:43 +00:00
refactor: move user schema to core
This commit is contained in:
@@ -16,7 +16,6 @@ import type {
|
|||||||
} from "./types";
|
} from "./types";
|
||||||
import { colors } from "../../utils/colors";
|
import { colors } from "../../utils/colors";
|
||||||
import type { DBFieldAttribute } from "@better-auth/core/db";
|
import type { DBFieldAttribute } from "@better-auth/core/db";
|
||||||
import { parseUserInput } from "../../db";
|
|
||||||
export * from "./types";
|
export * from "./types";
|
||||||
|
|
||||||
let debugLogs: { instance: string; args: any[] }[] = [];
|
let debugLogs: { instance: string; args: any[] }[] = [];
|
||||||
|
|||||||
@@ -3,13 +3,7 @@ import type { AuthPluginSchema } from "../types/plugins";
|
|||||||
import type { BetterAuthOptions } from "../types/options";
|
import type { BetterAuthOptions } from "../types/options";
|
||||||
import { APIError } from "better-call";
|
import { APIError } from "better-call";
|
||||||
import type { Account, Session, User } from "../types";
|
import type { Account, Session, User } from "../types";
|
||||||
import type { DBFieldAttribute } from "@better-auth/core/db";
|
import { type DBFieldAttribute, coreSchema } 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()),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const accountSchema = coreSchema.extend({
|
export const accountSchema = coreSchema.extend({
|
||||||
providerId: z.string(),
|
providerId: z.string(),
|
||||||
@@ -36,13 +30,6 @@ export const accountSchema = coreSchema.extend({
|
|||||||
password: z.string().nullish(),
|
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({
|
export const sessionSchema = coreSchema.extend({
|
||||||
userId: z.coerce.string(),
|
userId: z.coerce.string(),
|
||||||
expiresAt: z.date(),
|
expiresAt: z.date(),
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import type { BetterAuthOptions } from "./options";
|
|||||||
import type {
|
import type {
|
||||||
accountSchema,
|
accountSchema,
|
||||||
sessionSchema,
|
sessionSchema,
|
||||||
userSchema,
|
|
||||||
verificationSchema,
|
verificationSchema,
|
||||||
} from "../db/schema";
|
} from "../db/schema";
|
||||||
import type { Auth } from "../auth";
|
import type { Auth } from "../auth";
|
||||||
@@ -10,6 +9,7 @@ import type { InferFieldsFromOptions, InferFieldsFromPlugins } from "../db";
|
|||||||
import type { StripEmptyObjects, UnionToIntersection } from "./helper";
|
import type { StripEmptyObjects, UnionToIntersection } from "./helper";
|
||||||
import type { BetterAuthPlugin } from "./plugins";
|
import type { BetterAuthPlugin } from "./plugins";
|
||||||
import type * as z from "zod";
|
import type * as z from "zod";
|
||||||
|
import type { User } from "@better-auth/core/db";
|
||||||
|
|
||||||
export type Models =
|
export type Models =
|
||||||
| "user"
|
| "user"
|
||||||
@@ -89,7 +89,7 @@ interface RateLimit {
|
|||||||
lastRequest: number;
|
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 Account = z.infer<typeof accountSchema>;
|
||||||
export type Session = z.infer<typeof sessionSchema>;
|
export type Session = z.infer<typeof sessionSchema>;
|
||||||
export type Verification = z.infer<typeof verificationSchema>;
|
export type Verification = z.infer<typeof verificationSchema>;
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ import type {
|
|||||||
BetterAuthDBSchema,
|
BetterAuthDBSchema,
|
||||||
} from "./type";
|
} from "./type";
|
||||||
|
|
||||||
|
export { coreSchema } from "./schema/shared";
|
||||||
|
export { userSchema, type User } from "./schema/user";
|
||||||
|
|
||||||
export type {
|
export type {
|
||||||
DBFieldAttribute,
|
DBFieldAttribute,
|
||||||
DBFieldAttributeConfig,
|
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