Skip to content

Commit 4410a49

Browse files
authored
refactor: simplify collection, global and auth operations (#11374)
Continuation of #11372 but for our collection, global and auth operations Previously, we were quite frequently using `.reduce()` to run hooks. This PR replaces them with simple `for` loops, which is less overhead, less code, less confusing and simpler to understand.
1 parent 820a6ec commit 4410a49

29 files changed

+585
-628
lines changed
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
// @ts-strict-ignore
21
import type { AuthStrategyFunctionArgs, AuthStrategyResult } from './index.js'
3-
42
export const executeAuthStrategies = async (
53
args: AuthStrategyFunctionArgs,
64
): Promise<AuthStrategyResult> => {
7-
return args.payload.authStrategies.reduce(
8-
async (accumulatorPromise, strategy) => {
9-
const result: AuthStrategyResult = await accumulatorPromise
10-
if (!result.user) {
11-
// add the configured AuthStrategy `name` to the strategy function args
12-
args.strategyName = strategy.name
5+
if (!args.payload.authStrategies?.length) {
6+
return { user: null }
7+
}
8+
9+
for (const strategy of args.payload.authStrategies) {
10+
// add the configured AuthStrategy `name` to the strategy function args
11+
args.strategyName = strategy.name
1312

14-
return strategy.authenticate(args)
15-
}
13+
const result = await strategy.authenticate(args)
14+
if (result.user) {
1615
return result
17-
},
18-
Promise.resolve({ user: null }),
19-
)
16+
}
17+
}
18+
return { user: null }
2019
}

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,18 @@ export const forgotPasswordOperation = async <TSlug extends CollectionSlug>(
6464
// beforeOperation - Collection
6565
// /////////////////////////////////////
6666

67-
await args.collection.config.hooks.beforeOperation.reduce(async (priorHook, hook) => {
68-
await priorHook
69-
70-
args =
71-
(await hook({
72-
args,
73-
collection: args.collection?.config,
74-
context: args.req.context,
75-
operation: 'forgotPassword',
76-
req: args.req,
77-
})) || args
78-
}, Promise.resolve())
67+
if (args.collection.config.hooks?.beforeOperation?.length) {
68+
for (const hook of args.collection.config.hooks.beforeOperation) {
69+
args =
70+
(await hook({
71+
args,
72+
collection: args.collection?.config,
73+
context: args.req.context,
74+
operation: 'forgotPassword',
75+
req: args.req,
76+
})) || args
77+
}
78+
}
7979

8080
const {
8181
collection: { config: collectionConfig },
@@ -190,10 +190,11 @@ export const forgotPasswordOperation = async <TSlug extends CollectionSlug>(
190190
// afterForgotPassword - Collection
191191
// /////////////////////////////////////
192192

193-
await collectionConfig.hooks.afterForgotPassword.reduce(async (priorHook, hook) => {
194-
await priorHook
195-
await hook({ args, collection: args.collection?.config, context: req.context })
196-
}, Promise.resolve())
193+
if (collectionConfig.hooks?.afterForgotPassword?.length) {
194+
for (const hook of collectionConfig.hooks.afterForgotPassword) {
195+
await hook({ args, collection: args.collection?.config, context: req.context })
196+
}
197+
}
197198

198199
// /////////////////////////////////////
199200
// afterOperation - Collection

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

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,18 @@ export const loginOperation = async <TSlug extends CollectionSlug>(
5151
// beforeOperation - Collection
5252
// /////////////////////////////////////
5353

54-
await args.collection.config.hooks.beforeOperation.reduce(async (priorHook, hook) => {
55-
await priorHook
56-
57-
args =
58-
(await hook({
59-
args,
60-
collection: args.collection?.config,
61-
context: args.req.context,
62-
operation: 'login',
63-
req: args.req,
64-
})) || args
65-
}, Promise.resolve())
54+
if (args.collection.config.hooks?.beforeOperation?.length) {
55+
for (const hook of args.collection.config.hooks.beforeOperation) {
56+
args =
57+
(await hook({
58+
args,
59+
collection: args.collection?.config,
60+
context: args.req.context,
61+
operation: 'login',
62+
req: args.req,
63+
})) || args
64+
}
65+
}
6666

6767
const {
6868
collection: { config: collectionConfig },
@@ -227,17 +227,17 @@ export const loginOperation = async <TSlug extends CollectionSlug>(
227227
// beforeLogin - Collection
228228
// /////////////////////////////////////
229229

230-
await collectionConfig.hooks.beforeLogin.reduce(async (priorHook, hook) => {
231-
await priorHook
232-
233-
user =
234-
(await hook({
235-
collection: args.collection?.config,
236-
context: args.req.context,
237-
req: args.req,
238-
user,
239-
})) || user
240-
}, Promise.resolve())
230+
if (collectionConfig.hooks?.beforeLogin?.length) {
231+
for (const hook of collectionConfig.hooks.beforeLogin) {
232+
user =
233+
(await hook({
234+
collection: args.collection?.config,
235+
context: args.req.context,
236+
req: args.req,
237+
user,
238+
})) || user
239+
}
240+
}
241241

242242
const { exp, token } = await jwtSign({
243243
fieldsToSign,
@@ -251,18 +251,18 @@ export const loginOperation = async <TSlug extends CollectionSlug>(
251251
// afterLogin - Collection
252252
// /////////////////////////////////////
253253

254-
await collectionConfig.hooks.afterLogin.reduce(async (priorHook, hook) => {
255-
await priorHook
256-
257-
user =
258-
(await hook({
259-
collection: args.collection?.config,
260-
context: args.req.context,
261-
req: args.req,
262-
token,
263-
user,
264-
})) || user
265-
}, Promise.resolve())
254+
if (collectionConfig.hooks?.afterLogin?.length) {
255+
for (const hook of collectionConfig.hooks.afterLogin) {
256+
user =
257+
(await hook({
258+
collection: args.collection?.config,
259+
context: args.req.context,
260+
req: args.req,
261+
token,
262+
user,
263+
})) || user
264+
}
265+
}
266266

267267
// /////////////////////////////////////
268268
// afterRead - Fields
@@ -286,17 +286,17 @@ export const loginOperation = async <TSlug extends CollectionSlug>(
286286
// afterRead - Collection
287287
// /////////////////////////////////////
288288

289-
await collectionConfig.hooks.afterRead.reduce(async (priorHook, hook) => {
290-
await priorHook
291-
292-
user =
293-
(await hook({
294-
collection: args.collection?.config,
295-
context: req.context,
296-
doc: user,
297-
req,
298-
})) || user
299-
}, Promise.resolve())
289+
if (collectionConfig.hooks?.afterRead?.length) {
290+
for (const hook of collectionConfig.hooks.afterRead) {
291+
user =
292+
(await hook({
293+
collection: args.collection?.config,
294+
context: req.context,
295+
doc: user,
296+
req,
297+
})) || user
298+
}
299+
}
300300

301301
let result: { user: DataFromCollectionSlug<TSlug> } & Result = {
302302
exp,

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ export const logoutOperation = async (incomingArgs: Arguments): Promise<boolean>
2525
throw new APIError('Incorrect collection', httpStatus.FORBIDDEN)
2626
}
2727

28-
await collectionConfig.hooks.afterLogout.reduce(async (priorHook, hook) => {
29-
await priorHook
30-
31-
args =
32-
(await hook({
33-
collection: args.collection?.config,
34-
context: req.context,
35-
req,
36-
})) || args
37-
}, Promise.resolve())
28+
if (collectionConfig.hooks?.afterLogout?.length) {
29+
for (const hook of collectionConfig.hooks.afterLogout) {
30+
args =
31+
(await hook({
32+
collection: args.collection?.config,
33+
context: req.context,
34+
req,
35+
})) || args
36+
}
37+
}
3838

3939
return true
4040
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,17 @@ export const meOperation = async (args: Arguments): Promise<MeOperationResult> =
8686
// After Me - Collection
8787
// /////////////////////////////////////
8888

89-
await collection.config.hooks.afterMe.reduce(async (priorHook, hook) => {
90-
await priorHook
91-
92-
result =
93-
(await hook({
94-
collection: collection?.config,
95-
context: req.context,
96-
req,
97-
response: result,
98-
})) || result
99-
}, Promise.resolve())
89+
if (collection.config.hooks?.afterMe?.length) {
90+
for (const hook of collection.config.hooks.afterMe) {
91+
result =
92+
(await hook({
93+
collection: collection?.config,
94+
context: req.context,
95+
req,
96+
response: result,
97+
})) || result
98+
}
99+
}
100100

101101
return result
102102
}

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

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ export const refreshOperation = async (incomingArgs: Arguments): Promise<Result>
3535
// beforeOperation - Collection
3636
// /////////////////////////////////////
3737

38-
await args.collection.config.hooks.beforeOperation.reduce(
39-
async (priorHook: BeforeOperationHook | Promise<void>, hook: BeforeOperationHook) => {
40-
await priorHook
41-
38+
if (args.collection.config.hooks?.beforeOperation?.length) {
39+
for (const hook of args.collection.config.hooks.beforeOperation) {
4240
args =
4341
(await hook({
4442
args,
@@ -47,9 +45,8 @@ export const refreshOperation = async (incomingArgs: Arguments): Promise<Result>
4745
operation: 'refresh',
4846
req: args.req,
4947
})) || args
50-
},
51-
Promise.resolve(),
52-
)
48+
}
49+
}
5350

5451
// /////////////////////////////////////
5552
// Refresh
@@ -122,18 +119,18 @@ export const refreshOperation = async (incomingArgs: Arguments): Promise<Result>
122119
// After Refresh - Collection
123120
// /////////////////////////////////////
124121

125-
await collectionConfig.hooks.afterRefresh.reduce(async (priorHook, hook) => {
126-
await priorHook
127-
128-
result =
129-
(await hook({
130-
collection: args.collection?.config,
131-
context: args.req.context,
132-
exp: result.exp,
133-
req: args.req,
134-
token: result.refreshedToken,
135-
})) || result
136-
}, Promise.resolve())
122+
if (collectionConfig.hooks?.afterRefresh?.length) {
123+
for (const hook of collectionConfig.hooks.afterRefresh) {
124+
result =
125+
(await hook({
126+
collection: args.collection?.config,
127+
context: args.req.context,
128+
exp: result.exp,
129+
req: args.req,
130+
token: result.refreshedToken,
131+
})) || result
132+
}
133+
}
137134

138135
// /////////////////////////////////////
139136
// afterOperation - Collection

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,17 @@ export const resetPasswordOperation = async (args: Arguments): Promise<Result> =
9191
// beforeValidate - Collection
9292
// /////////////////////////////////////
9393

94-
await collectionConfig.hooks.beforeValidate.reduce(async (priorHook, hook) => {
95-
await priorHook
96-
97-
await hook({
98-
collection: args.collection?.config,
99-
context: req.context,
100-
data: user,
101-
operation: 'update',
102-
req,
103-
})
104-
}, Promise.resolve())
94+
if (collectionConfig.hooks?.beforeValidate?.length) {
95+
for (const hook of collectionConfig.hooks.beforeValidate) {
96+
await hook({
97+
collection: args.collection?.config,
98+
context: req.context,
99+
data: user,
100+
operation: 'update',
101+
req,
102+
})
103+
}
104+
}
105105

106106
// /////////////////////////////////////
107107
// Update new password

0 commit comments

Comments
 (0)