Skip to content

Commit 54a0484

Browse files
feat: adds calling of before and after operation hooks to resetPassword (#12581)
1 parent f2b54b5 commit 54a0484

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

packages/payload/src/auth/operations/local/resetPassword.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export type Options<T extends CollectionSlug> = {
1717
req?: Partial<PayloadRequest>
1818
}
1919

20-
async function localResetPassword<T extends CollectionSlug>(
20+
async function localResetPassword<TSlug extends CollectionSlug>(
2121
payload: Payload,
22-
options: Options<T>,
22+
options: Options<TSlug>,
2323
): Promise<Result> {
2424
const { collection: collectionSlug, data, overrideAccess } = options
2525

@@ -33,7 +33,7 @@ async function localResetPassword<T extends CollectionSlug>(
3333
)
3434
}
3535

36-
const result = await resetPasswordOperation({
36+
const result = await resetPasswordOperation<TSlug>({
3737
collection,
3838
data,
3939
overrideAccess,

packages/payload/src/auth/operations/resetPassword.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { status as httpStatus } from 'http-status'
22

3-
import type { Collection } from '../../collections/config/types.js'
3+
import type { Collection, DataFromCollectionSlug } from '../../collections/config/types.js'
4+
import type { CollectionSlug } from '../../index.js'
45
import type { PayloadRequest } from '../../types/index.js'
56

7+
import { buildAfterOperation } from '../../collections/operations/utils.js'
68
import { APIError, Forbidden } from '../../errors/index.js'
79
import { commitTransaction } from '../../utilities/commitTransaction.js'
810
import { initTransaction } from '../../utilities/initTransaction.js'
@@ -28,7 +30,9 @@ export type Arguments = {
2830
req: PayloadRequest
2931
}
3032

31-
export const resetPasswordOperation = async (args: Arguments): Promise<Result> => {
33+
export const resetPasswordOperation = async <TSlug extends CollectionSlug>(
34+
args: Arguments,
35+
): Promise<Result> => {
3236
const {
3337
collection: { config: collectionConfig },
3438
data,
@@ -55,6 +59,19 @@ export const resetPasswordOperation = async (args: Arguments): Promise<Result> =
5559
try {
5660
const shouldCommit = await initTransaction(req)
5761

62+
if (args.collection.config.hooks?.beforeOperation?.length) {
63+
for (const hook of args.collection.config.hooks.beforeOperation) {
64+
args =
65+
(await hook({
66+
args,
67+
collection: args.collection?.config,
68+
context: args.req.context,
69+
operation: 'resetPassword',
70+
req: args.req,
71+
})) || args
72+
}
73+
}
74+
5875
// /////////////////////////////////////
5976
// Reset Password
6077
// /////////////////////////////////////
@@ -135,6 +152,7 @@ export const resetPasswordOperation = async (args: Arguments): Promise<Result> =
135152
overrideAccess,
136153
req,
137154
})
155+
138156
if (shouldCommit) {
139157
await commitTransaction(req)
140158
}
@@ -144,11 +162,22 @@ export const resetPasswordOperation = async (args: Arguments): Promise<Result> =
144162
fullUser._strategy = 'local-jwt'
145163
}
146164

147-
const result = {
165+
let result: { user: DataFromCollectionSlug<TSlug> } & Result = {
148166
token,
149167
user: fullUser,
150168
}
151169

170+
// /////////////////////////////////////
171+
// afterOperation - Collection
172+
// /////////////////////////////////////
173+
174+
result = await buildAfterOperation({
175+
args,
176+
collection: args.collection?.config,
177+
operation: 'resetPassword',
178+
result,
179+
})
180+
152181
return result
153182
} catch (error: unknown) {
154183
await killTransaction(req)

packages/payload/src/collections/config/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export type HookOperationType =
8282
| 'login'
8383
| 'read'
8484
| 'refresh'
85+
| 'resetPassword'
8586
| 'update'
8687

8788
type CreateOrUpdateOperation = Extract<HookOperationType, 'create' | 'update'>

packages/payload/src/collections/operations/utils.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import type { forgotPasswordOperation } from '../../auth/operations/forgotPassword.js'
22
import type { loginOperation } from '../../auth/operations/login.js'
33
import type { refreshOperation } from '../../auth/operations/refresh.js'
4+
import type { resetPasswordOperation } from '../../auth/operations/resetPassword.js'
45
import type { CollectionSlug } from '../../index.js'
56
import type { PayloadRequest } from '../../types/index.js'
6-
import type {
7-
AfterOperationHook,
8-
SanitizedCollectionConfig,
9-
SelectFromCollectionSlug,
10-
} from '../config/types.js'
7+
import type { SanitizedCollectionConfig, SelectFromCollectionSlug } from '../config/types.js'
118
import type { countOperation } from './count.js'
129
import type { countVersionsOperation } from './countVersions.js'
1310
import type { createOperation } from './create.js'
@@ -36,6 +33,7 @@ export type AfterOperationMap<TOperationGeneric extends CollectionSlug> = {
3633
forgotPassword: typeof forgotPasswordOperation
3734
login: typeof loginOperation<TOperationGeneric>
3835
refresh: typeof refreshOperation
36+
resetPassword: typeof resetPasswordOperation<TOperationGeneric>
3937
update: typeof updateOperation<TOperationGeneric, SelectFromCollectionSlug<TOperationGeneric>>
4038
updateByID: typeof updateByIDOperation<
4139
TOperationGeneric,
@@ -98,6 +96,11 @@ export type AfterOperationArg<TOperationGeneric extends CollectionSlug> = {
9896
operation: 'refresh'
9997
result: Awaited<ReturnType<AfterOperationMap<TOperationGeneric>['refresh']>>
10098
}
99+
| {
100+
args: Parameters<AfterOperationMap<TOperationGeneric>['resetPassword']>[0]
101+
operation: 'resetPassword'
102+
result: Awaited<ReturnType<AfterOperationMap<TOperationGeneric>['resetPassword']>>
103+
}
101104
| {
102105
args: Parameters<AfterOperationMap<TOperationGeneric>['update']>[0]
103106
operation: 'update'

0 commit comments

Comments
 (0)