feat: adds calling of before and after operation hooks to resetPassword (#12581)

This commit is contained in:
Jarrod Flesch
2025-05-28 15:18:49 -04:00
committed by GitHub
parent f2b54b5b43
commit 54a04840c7
4 changed files with 44 additions and 11 deletions

View File

@@ -17,9 +17,9 @@ export type Options<T extends CollectionSlug> = {
req?: Partial<PayloadRequest>
}
async function localResetPassword<T extends CollectionSlug>(
async function localResetPassword<TSlug extends CollectionSlug>(
payload: Payload,
options: Options<T>,
options: Options<TSlug>,
): Promise<Result> {
const { collection: collectionSlug, data, overrideAccess } = options
@@ -33,7 +33,7 @@ async function localResetPassword<T extends CollectionSlug>(
)
}
const result = await resetPasswordOperation({
const result = await resetPasswordOperation<TSlug>({
collection,
data,
overrideAccess,

View File

@@ -1,8 +1,10 @@
import { status as httpStatus } from 'http-status'
import type { Collection } from '../../collections/config/types.js'
import type { Collection, DataFromCollectionSlug } from '../../collections/config/types.js'
import type { CollectionSlug } from '../../index.js'
import type { PayloadRequest } from '../../types/index.js'
import { buildAfterOperation } from '../../collections/operations/utils.js'
import { APIError, Forbidden } from '../../errors/index.js'
import { commitTransaction } from '../../utilities/commitTransaction.js'
import { initTransaction } from '../../utilities/initTransaction.js'
@@ -28,7 +30,9 @@ export type Arguments = {
req: PayloadRequest
}
export const resetPasswordOperation = async (args: Arguments): Promise<Result> => {
export const resetPasswordOperation = async <TSlug extends CollectionSlug>(
args: Arguments,
): Promise<Result> => {
const {
collection: { config: collectionConfig },
data,
@@ -55,6 +59,19 @@ export const resetPasswordOperation = async (args: Arguments): Promise<Result> =
try {
const shouldCommit = await initTransaction(req)
if (args.collection.config.hooks?.beforeOperation?.length) {
for (const hook of args.collection.config.hooks.beforeOperation) {
args =
(await hook({
args,
collection: args.collection?.config,
context: args.req.context,
operation: 'resetPassword',
req: args.req,
})) || args
}
}
// /////////////////////////////////////
// Reset Password
// /////////////////////////////////////
@@ -135,6 +152,7 @@ export const resetPasswordOperation = async (args: Arguments): Promise<Result> =
overrideAccess,
req,
})
if (shouldCommit) {
await commitTransaction(req)
}
@@ -144,11 +162,22 @@ export const resetPasswordOperation = async (args: Arguments): Promise<Result> =
fullUser._strategy = 'local-jwt'
}
const result = {
let result: { user: DataFromCollectionSlug<TSlug> } & Result = {
token,
user: fullUser,
}
// /////////////////////////////////////
// afterOperation - Collection
// /////////////////////////////////////
result = await buildAfterOperation({
args,
collection: args.collection?.config,
operation: 'resetPassword',
result,
})
return result
} catch (error: unknown) {
await killTransaction(req)

View File

@@ -82,6 +82,7 @@ export type HookOperationType =
| 'login'
| 'read'
| 'refresh'
| 'resetPassword'
| 'update'
type CreateOrUpdateOperation = Extract<HookOperationType, 'create' | 'update'>

View File

@@ -1,13 +1,10 @@
import type { forgotPasswordOperation } from '../../auth/operations/forgotPassword.js'
import type { loginOperation } from '../../auth/operations/login.js'
import type { refreshOperation } from '../../auth/operations/refresh.js'
import type { resetPasswordOperation } from '../../auth/operations/resetPassword.js'
import type { CollectionSlug } from '../../index.js'
import type { PayloadRequest } from '../../types/index.js'
import type {
AfterOperationHook,
SanitizedCollectionConfig,
SelectFromCollectionSlug,
} from '../config/types.js'
import type { SanitizedCollectionConfig, SelectFromCollectionSlug } from '../config/types.js'
import type { countOperation } from './count.js'
import type { countVersionsOperation } from './countVersions.js'
import type { createOperation } from './create.js'
@@ -36,6 +33,7 @@ export type AfterOperationMap<TOperationGeneric extends CollectionSlug> = {
forgotPassword: typeof forgotPasswordOperation
login: typeof loginOperation<TOperationGeneric>
refresh: typeof refreshOperation
resetPassword: typeof resetPasswordOperation<TOperationGeneric>
update: typeof updateOperation<TOperationGeneric, SelectFromCollectionSlug<TOperationGeneric>>
updateByID: typeof updateByIDOperation<
TOperationGeneric,
@@ -98,6 +96,11 @@ export type AfterOperationArg<TOperationGeneric extends CollectionSlug> = {
operation: 'refresh'
result: Awaited<ReturnType<AfterOperationMap<TOperationGeneric>['refresh']>>
}
| {
args: Parameters<AfterOperationMap<TOperationGeneric>['resetPassword']>[0]
operation: 'resetPassword'
result: Awaited<ReturnType<AfterOperationMap<TOperationGeneric>['resetPassword']>>
}
| {
args: Parameters<AfterOperationMap<TOperationGeneric>['update']>[0]
operation: 'update'