From 01d14d7228918f78fad4d72884b5db151dda4243 Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Thu, 2 Oct 2025 11:51:33 -0700 Subject: [PATCH] refactor: move account schema to core --- packages/better-auth/src/db/schema.ts | 25 ----------------- packages/better-auth/src/types/models.ts | 9 ++----- packages/core/src/db/index.ts | 1 + packages/core/src/db/schema/account.ts | 34 ++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 32 deletions(-) create mode 100644 packages/core/src/db/schema/account.ts diff --git a/packages/better-auth/src/db/schema.ts b/packages/better-auth/src/db/schema.ts index 2be86e44..e2d89dbf 100644 --- a/packages/better-auth/src/db/schema.ts +++ b/packages/better-auth/src/db/schema.ts @@ -5,31 +5,6 @@ import { APIError } from "better-call"; import type { Account, Session, User } from "../types"; import { type DBFieldAttribute, coreSchema } from "@better-auth/core/db"; -export const accountSchema = coreSchema.extend({ - providerId: z.string(), - accountId: z.string(), - userId: z.coerce.string(), - accessToken: z.string().nullish(), - refreshToken: z.string().nullish(), - idToken: z.string().nullish(), - /** - * Access token expires at - */ - accessTokenExpiresAt: z.date().nullish(), - /** - * Refresh token expires at - */ - refreshTokenExpiresAt: z.date().nullish(), - /** - * The scopes that the user has authorized - */ - scope: z.string().nullish(), - /** - * Password is only stored in the credential provider - */ - password: z.string().nullish(), -}); - export const sessionSchema = coreSchema.extend({ userId: z.coerce.string(), expiresAt: z.date(), diff --git a/packages/better-auth/src/types/models.ts b/packages/better-auth/src/types/models.ts index 78de826c..5e64c92f 100644 --- a/packages/better-auth/src/types/models.ts +++ b/packages/better-auth/src/types/models.ts @@ -1,9 +1,5 @@ import type { BetterAuthOptions } from "./options"; -import type { - accountSchema, - sessionSchema, - verificationSchema, -} from "../db/schema"; +import type { sessionSchema, verificationSchema } from "../db/schema"; import type { Auth } from "../auth"; import type { InferFieldsFromOptions, InferFieldsFromPlugins } from "../db"; import type { StripEmptyObjects, UnionToIntersection } from "./helper"; @@ -89,8 +85,7 @@ interface RateLimit { lastRequest: number; } -export type { User } from "@better-auth/core/db"; -export type Account = z.infer; +export type { User, Account } from "@better-auth/core/db"; export type Session = z.infer; export type Verification = z.infer; export type { RateLimit }; diff --git a/packages/core/src/db/index.ts b/packages/core/src/db/index.ts index e273f9f2..1a564648 100644 --- a/packages/core/src/db/index.ts +++ b/packages/core/src/db/index.ts @@ -8,6 +8,7 @@ import type { export { coreSchema } from "./schema/shared"; export { userSchema, type User } from "./schema/user"; +export { accountSchema, type Account } from "./schema/account"; export type { DBFieldAttribute, diff --git a/packages/core/src/db/schema/account.ts b/packages/core/src/db/schema/account.ts new file mode 100644 index 00000000..0d925b01 --- /dev/null +++ b/packages/core/src/db/schema/account.ts @@ -0,0 +1,34 @@ +import * as z from "zod"; +import { coreSchema } from "./shared"; + +export const accountSchema = coreSchema.extend({ + providerId: z.string(), + accountId: z.string(), + userId: z.coerce.string(), + accessToken: z.string().nullish(), + refreshToken: z.string().nullish(), + idToken: z.string().nullish(), + /** + * Access token expires at + */ + accessTokenExpiresAt: z.date().nullish(), + /** + * Refresh token expires at + */ + refreshTokenExpiresAt: z.date().nullish(), + /** + * The scopes that the user has authorized + */ + scope: z.string().nullish(), + /** + * Password is only stored in the credential provider + */ + password: z.string().nullish(), +}); + +/** + * Account schema type used by better-auth, note that it's possible that account 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 Account = z.infer;