better-auth
Version:
The most comprehensive authentication framework for TypeScript.
127 lines (126 loc) • 3.99 kB
text/typescript
import * as _$_better_auth_core0 from "@better-auth/core";
import { GenericEndpointContext } from "@better-auth/core";
import * as _$better_call0 from "better-call";
//#region src/plugins/last-login-method/index.d.ts
declare module "@better-auth/core" {
interface BetterAuthPluginRegistry<AuthOptions, Options> {
"last-login-method": {
creator: typeof lastLoginMethod;
};
}
}
/**
* Configuration for tracking different authentication methods
*/
interface LastLoginMethodOptions {
/**
* Name of the cookie to store the last login method
* @default "better-auth.last_used_login_method"
*/
cookieName?: string | undefined;
/**
* Cookie expiration time in seconds
* @default 2592000 (30 days)
*/
maxAge?: number | undefined;
/**
* Custom method to resolve the last login method
* @param ctx - The context from the hook
* @returns The last login method
*/
customResolveMethod?: ((ctx: GenericEndpointContext) => string | null) | undefined;
/**
* Store the last login method in the database. This will create a new field in the user table.
* @default false
*/
storeInDatabase?: boolean | undefined;
/**
* A hook to run before the last login method is stored in the cookie.
* Useful if you are required to follow GDPR or other regulations to ensure that you're allowed to store the last login method in the cookie.
*
* @param ctx - The context from the hook
* @param lastUsedLoginMethod - The last login method
* @returns `true` to store the cookie, `false` to skip storing it (authentication continues either way)
*/
beforeStoreCookie?: ((ctx: GenericEndpointContext, lastUsedLoginMethod: string) => Promise<boolean> | boolean) | undefined;
/**
* Custom schema for the plugin
* @default undefined
*/
schema?: {
user?: {
lastLoginMethod?: string;
};
} | undefined;
}
/**
* Plugin to track the last used login method
*/
declare const lastLoginMethod: <O extends LastLoginMethodOptions>(userConfig?: O | undefined) => {
id: "last-login-method";
version: string;
init(ctx: _$_better_auth_core0.AuthContext): {
options: {
databaseHooks: {
user: {
create: {
before(user: {
id: string;
createdAt: Date;
updatedAt: Date;
email: string;
emailVerified: boolean;
name: string;
image?: string | null | undefined;
} & Record<string, unknown>, context: GenericEndpointContext | null): Promise<{
data: {
lastLoginMethod: string;
id: string;
createdAt: Date;
updatedAt: Date;
email: string;
emailVerified: boolean;
name: string;
image?: string | null | undefined;
};
} | undefined>;
};
};
session: {
create: {
after(session: {
id: string;
createdAt: Date;
updatedAt: Date;
userId: string;
expiresAt: Date;
token: string;
ipAddress?: string | null | undefined;
userAgent?: string | null | undefined;
} & Record<string, unknown>, context: GenericEndpointContext | null): Promise<void>;
};
};
};
};
};
hooks: {
after: {
matcher(): true;
handler: _$better_call0.Middleware<_$better_call0.MiddlewareOptions, (inputContext: _$better_call0.MiddlewareInputContext<_$better_call0.MiddlewareOptions>) => Promise<void>>;
}[];
};
schema: O["storeInDatabase"] extends true ? {
user: {
fields: {
lastLoginMethod: {
type: "string";
required: false;
input: false;
};
};
};
} : undefined;
options: NoInfer<O>;
};
//#endregion
export { LastLoginMethodOptions, lastLoginMethod };