fix: db hooks

This commit is contained in:
Bereket Engida
2024-10-02 20:18:32 +03:00
parent 80e7c5fcfc
commit aa6fd9cab7
6 changed files with 32 additions and 14 deletions
+2 -2
View File
@@ -351,7 +351,7 @@ There are two types of hooks you can define:
import { betterAuth } from "better-auth";
export const auth = betterAuth({
databaseHooks: [{
databaseHooks: {
user: {
create: {
before: async (user) => {
@@ -367,6 +367,6 @@ export const auth = betterAuth({
},
},
},
}]
}
})
```
@@ -8,11 +8,15 @@ import { getWithHooks } from "./with-hooks";
export const createInternalAdapter = (
adapter: Adapter,
options: BetterAuthOptions,
ctx: {
options: BetterAuthOptions;
hooks: Exclude<BetterAuthOptions["databaseHooks"], undefined>[];
},
) => {
const options = ctx.options;
const sessionExpiration = options.session?.expiresIn || 60 * 60 * 24 * 7; // 7 days
const tables = getAuthTables(options);
const { createWithHooks, updateWithHooks } = getWithHooks(adapter, options);
const { createWithHooks, updateWithHooks } = getWithHooks(adapter, ctx);
return {
createOAuthUser: async (user: User, account: Account) => {
try {
+9 -3
View File
@@ -3,9 +3,15 @@ import type { BetterAuthOptions } from "../types";
import { getAuthTables } from "./get-tables";
import { generateId } from "../utils";
export function getWithHooks(adapter: Adapter, options: BetterAuthOptions) {
const hooks = options.databaseHooks;
const tables = getAuthTables(options);
export function getWithHooks(
adapter: Adapter,
ctx: {
options: BetterAuthOptions;
hooks: Exclude<BetterAuthOptions["databaseHooks"], undefined>[];
},
) {
const hooks = ctx.hooks;
const tables = getAuthTables(ctx.options);
type Models = "user" | "account" | "session" | "verification";
async function createWithHooks<T extends Record<string, any>>(
data: T,
+10 -2
View File
@@ -27,7 +27,7 @@ export const init = async (opts: BetterAuthOptions) => {
/**
* Run plugins init to get the actual options
*/
const { options, context } = runPluginInit(opts);
const { options, context, dbHooks } = runPluginInit(opts);
const plugins = options.plugins || [];
const internalPlugins = getInternalPlugins(options);
const adapter = await getAdapter(options);
@@ -96,7 +96,10 @@ export const init = async (opts: BetterAuthOptions) => {
},
},
adapter: adapter,
internalAdapter: createInternalAdapter(adapter, options),
internalAdapter: createInternalAdapter(adapter, {
options,
hooks: dbHooks.filter((u) => u !== undefined),
}),
createAuthCookie: createCookieGetter(options),
...context,
};
@@ -138,11 +141,15 @@ export type AuthContext = {
function runPluginInit(options: BetterAuthOptions) {
const plugins = options.plugins || [];
let context: Partial<AuthContext> = {};
const dbHooks: BetterAuthOptions["databaseHooks"][] = [options.databaseHooks];
for (const plugin of plugins) {
if (plugin.init) {
const result = plugin.init(options);
if (typeof result === "object") {
if (result.options) {
if (result.options.databaseHooks) {
dbHooks.push(result.options.databaseHooks);
}
options = defu(options, result.options);
}
if (result.context) {
@@ -154,6 +161,7 @@ function runPluginInit(options: BetterAuthOptions) {
return {
options,
context,
dbHooks,
};
}
+1 -1
View File
@@ -385,5 +385,5 @@ export interface BetterAuthOptions {
after?: (verification: Verification) => Promise<void>;
};
};
}[];
};
}
+4 -4
View File
@@ -21,10 +21,10 @@ export type BetterAuthPlugin = {
* The init function is called when the plugin is initialized.
* You can return a new context or modify the existing context.
*/
init?: (options: BetterAuthOptions) => DeepPartial<{
context: Omit<AuthContext, "options">;
options: BetterAuthOptions;
}> | void;
init?: (options: BetterAuthOptions) => {
context?: DeepPartial<Omit<AuthContext, "options">>;
options?: BetterAuthOptions;
} | void;
endpoints?: {
[key: string]: AuthEndpoint;
};