refactor: move account schema to core

This commit is contained in:
Alex Yang
2025-10-02 11:51:33 -07:00
parent a6c000d428
commit 01d14d7228
4 changed files with 37 additions and 32 deletions

View File

@@ -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(),

View File

@@ -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<typeof accountSchema>;
export type { User, Account } from "@better-auth/core/db";
export type Session = z.infer<typeof sessionSchema>;
export type Verification = z.infer<typeof verificationSchema>;
export type { RateLimit };

View File

@@ -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,

View File

@@ -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<typeof accountSchema>;