From 625ea8c040eb63d14a7cd658bd58c9bcd6aea5ef Mon Sep 17 00:00:00 2001 From: Bereket Engida Date: Mon, 18 Nov 2024 15:29:23 +0300 Subject: [PATCH] fix: use update many for update password --- .../src/api/routes/forget-password.ts | 10 +--- .../better-auth/src/db/internal-adapter.ts | 6 +-- packages/better-auth/src/db/with-hooks.ts | 47 +++++++++++++++++++ 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/packages/better-auth/src/api/routes/forget-password.ts b/packages/better-auth/src/api/routes/forget-password.ts index 8066bdab7e..594eb9f7c6 100644 --- a/packages/better-auth/src/api/routes/forget-password.ts +++ b/packages/better-auth/src/api/routes/forget-password.ts @@ -187,15 +187,7 @@ export const resetPassword = createAuthEndpoint( status: true, }); } - const updatedUser = await ctx.context.internalAdapter.updatePassword( - userId, - hashedPassword, - ); - if (!updatedUser) { - throw new APIError("BAD_REQUEST", { - message: "Failed to update password", - }); - } + await ctx.context.internalAdapter.updatePassword(userId, hashedPassword); return ctx.json({ status: true, }); diff --git a/packages/better-auth/src/db/internal-adapter.ts b/packages/better-auth/src/db/internal-adapter.ts index b29ac05b56..a61ed77663 100644 --- a/packages/better-auth/src/db/internal-adapter.ts +++ b/packages/better-auth/src/db/internal-adapter.ts @@ -24,7 +24,8 @@ export const createInternalAdapter = ( const options = ctx.options; const secondaryStorage = options.secondaryStorage; const sessionExpiration = options.session?.expiresIn || 60 * 60 * 24 * 7; // 7 days - const { createWithHooks, updateWithHooks } = getWithHooks(adapter, ctx); + const { createWithHooks, updateWithHooks, updateManyWithHooks } = + getWithHooks(adapter, ctx); return { createOAuthUser: async ( user: Omit & Partial, @@ -580,7 +581,7 @@ export const createInternalAdapter = ( return user; }, updatePassword: async (userId: string, password: string) => { - const account = await updateWithHooks( + await updateManyWithHooks( { password, }, @@ -596,7 +597,6 @@ export const createInternalAdapter = ( ], "account", ); - return account; }, findAccounts: async (userId: string) => { const accounts = await adapter.findMany({ diff --git a/packages/better-auth/src/db/with-hooks.ts b/packages/better-auth/src/db/with-hooks.ts index b0370ddbdc..11227d59ca 100644 --- a/packages/better-auth/src/db/with-hooks.ts +++ b/packages/better-auth/src/db/with-hooks.ts @@ -99,8 +99,55 @@ export function getWithHooks( return updated; } + + async function updateManyWithHooks>( + data: any, + where: Where[], + model: Models, + customUpdateFn?: { + fn: (data: Record) => void | Promise; + executeMainFn?: boolean; + }, + ) { + let actualData = data; + + for (const hook of hooks || []) { + const toRun = hook[model]?.update?.before; + if (toRun) { + const result = await toRun(data as any); + if (result === false) { + return null; + } + const isObject = typeof result === "object"; + actualData = isObject ? (result as any).data : result; + } + } + + const customUpdated = customUpdateFn + ? await customUpdateFn.fn(actualData) + : null; + + const updated = + !customUpdateFn || customUpdateFn.executeMainFn + ? await adapter.updateMany({ + model, + update: actualData, + where, + }) + : customUpdated; + + for (const hook of hooks || []) { + const toRun = hook[model]?.update?.after; + if (toRun) { + await toRun(updated as any); + } + } + + return updated; + } return { createWithHooks, updateWithHooks, + updateManyWithHooks, }; }