UNPKG

better-auth-credentials-plugin

Version:
402 lines (401 loc) 17 kB
import { EndpointContext } from "better-call"; import { Account, User } from "better-auth"; import { default as z, ZodTypeAny } from "zod"; declare const defaultCredentialsSchema: z.ZodObject<{ email: z.ZodString; password: z.ZodString; rememberMe: z.ZodOptional<z.ZodBoolean>; }, "strip", z.ZodTypeAny, { email: string; password: string; rememberMe?: boolean | undefined; }, { email: string; password: string; rememberMe?: boolean | undefined; }>; type DefaultCredentialsType = z.infer<typeof defaultCredentialsSchema>; type GetBodyParsed<Z> = Z extends z.ZodTypeAny ? z.infer<Z> : DefaultCredentialsType; type MaybePromise<T> = T | Promise<T>; export type CallbackResult<U extends User> = (Partial<U> & { onSignUp?: (userData: Partial<U>) => MaybePromise<Partial<U>>; onSignIn?: (userData: Partial<U>, user: U, account: Account) => MaybePromise<Partial<U>>; }) | null | undefined; export type CredentialOptions<U extends User = User, Z extends (ZodTypeAny | undefined) = undefined> = { /** * Function that receives the credential and password and returns a Promise with the partial user data to be updated. * * If the user does not exists it will be created if `autoSignUp` is true, in this case a * the returned user data will be used to create the user, otherwise, if the user exists, it will be updated with the returned user data. * * If a custom inputSchema is set and it hasn't an `email` field, then you should return the `email` field to uniquely identify the user (Better auth can't operate without emails anyway). * * The `onSignIn` and `onSignUp` callbacks are optional, but if returned they will be called to handle updating the user data differently based if the user is signing in or signing up. */ callback: (ctx: EndpointContext<string, any>, parsed: GetBodyParsed<Z>) => MaybePromise<CallbackResult<U>>; /** * Schema for the input data, if not provided it will use the default schema allowing any non-empty string for credential and password */ inputSchema?: Z; /** * Whether to sign up the user if they successfully authenticate but do not exist locally * @default false */ autoSignUp?: boolean; /** * If is allowed to link an account to an existing user without an Account of this provider * * Basically, if the user already exists, but with another provider (e.g. email and password), if this is true a * new Account will be created and linked to this user (as if new login method), otherwise it will throw an error. * @default false */ linkAccountIfExisting?: boolean; /** * The Id of the provider to be used for the account created, fallback to "credential", the same used by the email and password flow. * * Obs: If you are using this plugin with the email and password plugin and did not change the providerId, users that have a password set will not be able to log in with this credentials plugin. * @default "credential" */ providerId?: string; /** * The path for the endpoint * @default "/sign-in/credentials" */ path?: string; /** * This is used to infer the User type to be used, never used otherwise. If not provided it will be the default User type. * * For example, to add a lastLogin input value: * > {} as User & {lastLogin: Date} */ UserType?: U; }; export declare const credentials: <U extends User = User, Z extends (ZodTypeAny | undefined) = undefined>(options: CredentialOptions<U, Z>) => { id: "credentials"; endpoints: { signInUsername: { <AsResponse extends boolean = false, ReturnHeaders extends boolean = false>(...inputCtx: import("better-call").HasRequiredKeys<import("better-call").InputContext<string, { method: "POST"; body: z.ZodObject<{ email: z.ZodString; password: z.ZodString; rememberMe: z.ZodOptional<z.ZodBoolean>; }, "strip", z.ZodTypeAny, { email: string; password: string; rememberMe?: boolean | undefined; }, { email: string; password: string; rememberMe?: boolean | undefined; }> | NonNullable<Z>; metadata: { openapi: { summary: string; description: string; responses: { 200: { description: string; content: { "application/json": { schema: { type: "object"; properties: { token: { type: string; description: string; }; user: { $ref: string; }; }; required: string[]; }; }; }; }; }; }; }; } & { use: any[]; }>> extends true ? [import("better-call").InferBodyInput<{ method: "POST"; body: z.ZodObject<{ email: z.ZodString; password: z.ZodString; rememberMe: z.ZodOptional<z.ZodBoolean>; }, "strip", z.ZodTypeAny, { email: string; password: string; rememberMe?: boolean | undefined; }, { email: string; password: string; rememberMe?: boolean | undefined; }> | NonNullable<Z>; metadata: { openapi: { summary: string; description: string; responses: { 200: { description: string; content: { "application/json": { schema: { type: "object"; properties: { token: { type: string; description: string; }; user: { $ref: string; }; }; required: string[]; }; }; }; }; }; }; }; } & { use: any[]; }, z.ZodObject<{ email: z.ZodString; password: z.ZodString; rememberMe: z.ZodOptional<z.ZodBoolean>; }, "strip", z.ZodTypeAny, { email: string; password: string; rememberMe?: boolean | undefined; }, { email: string; password: string; rememberMe?: boolean | undefined; }> | NonNullable<Z> extends import("better-call").StandardSchemaV1<unknown, unknown> ? import("better-call").StandardSchemaV1.InferInput<z.ZodObject<{ email: z.ZodString; password: z.ZodString; rememberMe: z.ZodOptional<z.ZodBoolean>; }, "strip", z.ZodTypeAny, { email: string; password: string; rememberMe?: boolean | undefined; }, { email: string; password: string; rememberMe?: boolean | undefined; }> | NonNullable<Z>> : undefined> & { method?: "POST" | undefined; } & { query?: Record<string, any> | undefined; } & { params?: Record<string, any>; } & { request?: Request; } & { headers?: HeadersInit; } & { asResponse?: boolean; returnHeaders?: boolean; use?: import("better-call").Middleware[]; path?: string; } & { asResponse?: AsResponse | undefined; returnHeaders?: ReturnHeaders | undefined; }] : [((import("better-call").InferBodyInput<{ method: "POST"; body: z.ZodObject<{ email: z.ZodString; password: z.ZodString; rememberMe: z.ZodOptional<z.ZodBoolean>; }, "strip", z.ZodTypeAny, { email: string; password: string; rememberMe?: boolean | undefined; }, { email: string; password: string; rememberMe?: boolean | undefined; }> | NonNullable<Z>; metadata: { openapi: { summary: string; description: string; responses: { 200: { description: string; content: { "application/json": { schema: { type: "object"; properties: { token: { type: string; description: string; }; user: { $ref: string; }; }; required: string[]; }; }; }; }; }; }; }; } & { use: any[]; }, z.ZodObject<{ email: z.ZodString; password: z.ZodString; rememberMe: z.ZodOptional<z.ZodBoolean>; }, "strip", z.ZodTypeAny, { email: string; password: string; rememberMe?: boolean | undefined; }, { email: string; password: string; rememberMe?: boolean | undefined; }> | NonNullable<Z> extends import("better-call").StandardSchemaV1<unknown, unknown> ? import("better-call").StandardSchemaV1.InferInput<z.ZodObject<{ email: z.ZodString; password: z.ZodString; rememberMe: z.ZodOptional<z.ZodBoolean>; }, "strip", z.ZodTypeAny, { email: string; password: string; rememberMe?: boolean | undefined; }, { email: string; password: string; rememberMe?: boolean | undefined; }> | NonNullable<Z>> : undefined> & { method?: "POST" | undefined; } & { query?: Record<string, any> | undefined; } & { params?: Record<string, any>; } & { request?: Request; } & { headers?: HeadersInit; } & { asResponse?: boolean; returnHeaders?: boolean; use?: import("better-call").Middleware[]; path?: string; } & { asResponse?: AsResponse | undefined; returnHeaders?: ReturnHeaders | undefined; }) | undefined)?]): Promise<[AsResponse] extends [true] ? Response : [ReturnHeaders] extends [true] ? { headers: Headers; response: { token: null; user: { id: string; email: string; name: string; image: string | null | undefined; emailVerified: false; createdAt: Date; updatedAt: Date; }; } | { token: string; user: { id: string; email: string; name: string; image: string | null | undefined; emailVerified: boolean; createdAt: Date; updatedAt: Date; }; }; } : { token: null; user: { id: string; email: string; name: string; image: string | null | undefined; emailVerified: false; createdAt: Date; updatedAt: Date; }; } | { token: string; user: { id: string; email: string; name: string; image: string | null | undefined; emailVerified: boolean; createdAt: Date; updatedAt: Date; }; }>; options: { method: "POST"; body: z.ZodObject<{ email: z.ZodString; password: z.ZodString; rememberMe: z.ZodOptional<z.ZodBoolean>; }, "strip", z.ZodTypeAny, { email: string; password: string; rememberMe?: boolean | undefined; }, { email: string; password: string; rememberMe?: boolean | undefined; }> | NonNullable<Z>; metadata: { openapi: { summary: string; description: string; responses: { 200: { description: string; content: { "application/json": { schema: { type: "object"; properties: { token: { type: string; description: string; }; user: { $ref: string; }; }; required: string[]; }; }; }; }; }; }; }; } & { use: any[]; }; path: string; }; }; $ERROR_CODES: { INVALID_CREDENTIALS: string; EMAIL_REQUIRED: string; EMAIL_NOT_VERIFIED: string; UNEXPECTED_ERROR: string; USERNAME_IS_ALREADY_TAKEN: string; }; }; export {};