Skip to content

Commit 1b5e3fe

Browse files
authored
fix(next): remove error handling from next auth functions (#12897)
The `@payloadcms/next/auth` functions are unnecessarily wrapped with `try...catch` blocks that propagate the original error as a plain string. This makes it impossible for the end user's error handling to differentiate between error types. These functions also throw errors regardless, and therefore must be wrapped with proper error handling anyway. Especially after removing the internal logging in #12881, these blocks do not serve any purpose. This PR also removes unused imports.
1 parent ca0d036 commit 1b5e3fe

File tree

3 files changed

+53
-64
lines changed

3 files changed

+53
-64
lines changed

packages/next/src/auth/login.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import type { CollectionSlug } from 'payload'
44

5-
import { cookies as getCookies } from 'next/headers.js'
6-
import { generatePayloadCookie, getPayload } from 'payload'
5+
import { getPayload } from 'payload'
76

87
import { setPayloadAuthCookie } from '../utilities/setPayloadAuthCookie.js'
98

@@ -31,6 +30,7 @@ export async function login({ collection, config, email, password, username }: L
3130
const payload = await getPayload({ config })
3231

3332
const authConfig = payload.collections[collection]?.config.auth
33+
3434
if (!authConfig) {
3535
throw new Error(`No auth config found for collection: ${collection}`)
3636
}
@@ -61,26 +61,22 @@ export async function login({ collection, config, email, password, username }: L
6161
loginData = { email, password }
6262
}
6363

64-
try {
65-
const result = await payload.login({
66-
collection,
67-
data: loginData,
68-
})
69-
70-
if (result.token) {
71-
await setPayloadAuthCookie({
72-
authConfig,
73-
cookiePrefix: payload.config.cookiePrefix,
74-
token: result.token,
75-
})
76-
}
64+
const result = await payload.login({
65+
collection,
66+
data: loginData,
67+
})
7768

78-
if ('removeTokenFromResponses' in config && config.removeTokenFromResponses) {
79-
delete result.token
80-
}
69+
if (result.token) {
70+
await setPayloadAuthCookie({
71+
authConfig,
72+
cookiePrefix: payload.config.cookiePrefix,
73+
token: result.token,
74+
})
75+
}
8176

82-
return result
83-
} catch (e) {
84-
throw new Error(`${e}`)
77+
if ('removeTokenFromResponses' in config && config.removeTokenFromResponses) {
78+
delete result.token
8579
}
80+
81+
return result
8682
}

packages/next/src/auth/logout.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,19 @@ import { getPayload } from 'payload'
66
import { getExistingAuthToken } from '../utilities/getExistingAuthToken.js'
77

88
export async function logout({ config }: { config: any }) {
9-
try {
10-
const payload = await getPayload({ config })
11-
const headers = await nextHeaders()
12-
const result = await payload.auth({ headers })
9+
const payload = await getPayload({ config })
10+
const headers = await nextHeaders()
11+
const result = await payload.auth({ headers })
1312

14-
if (!result.user) {
15-
return { message: 'User already logged out', success: true }
16-
}
13+
if (!result.user) {
14+
return { message: 'User already logged out', success: true }
15+
}
1716

18-
const existingCookie = await getExistingAuthToken(payload.config.cookiePrefix)
17+
const existingCookie = await getExistingAuthToken(payload.config.cookiePrefix)
1918

20-
if (existingCookie) {
21-
const cookies = await getCookies()
22-
cookies.delete(existingCookie.name)
23-
return { message: 'User logged out successfully', success: true }
24-
}
25-
} catch (e) {
26-
throw new Error(`${e}`)
19+
if (existingCookie) {
20+
const cookies = await getCookies()
21+
cookies.delete(existingCookie.name)
22+
return { message: 'User logged out successfully', success: true }
2723
}
2824
}

packages/next/src/auth/refresh.ts

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,30 @@ import { getExistingAuthToken } from '../utilities/getExistingAuthToken.js'
99
import { setPayloadAuthCookie } from '../utilities/setPayloadAuthCookie.js'
1010

1111
export async function refresh({ collection, config }: { collection: CollectionSlug; config: any }) {
12-
try {
13-
const payload = await getPayload({ config })
14-
const authConfig = payload.collections[collection]?.config.auth
15-
16-
if (!authConfig) {
17-
throw new Error(`No auth config found for collection: ${collection}`)
18-
}
19-
20-
const { user } = await payload.auth({ headers: await nextHeaders() })
21-
if (!user) {
22-
throw new Error('User not authenticated')
23-
}
24-
25-
const existingCookie = await getExistingAuthToken(payload.config.cookiePrefix)
26-
27-
if (!existingCookie) {
28-
return { message: 'No valid token found', success: false }
29-
}
30-
31-
await setPayloadAuthCookie({
32-
authConfig,
33-
cookiePrefix: payload.config.cookiePrefix,
34-
token: existingCookie.value,
35-
})
36-
37-
return { message: 'Token refreshed successfully', success: true }
38-
} catch (e) {
39-
throw new Error(`${e}`)
12+
const payload = await getPayload({ config })
13+
const authConfig = payload.collections[collection]?.config.auth
14+
15+
if (!authConfig) {
16+
throw new Error(`No auth config found for collection: ${collection}`)
4017
}
18+
19+
const { user } = await payload.auth({ headers: await nextHeaders() })
20+
21+
if (!user) {
22+
throw new Error('User not authenticated')
23+
}
24+
25+
const existingCookie = await getExistingAuthToken(payload.config.cookiePrefix)
26+
27+
if (!existingCookie) {
28+
return { message: 'No valid token found', success: false }
29+
}
30+
31+
await setPayloadAuthCookie({
32+
authConfig,
33+
cookiePrefix: payload.config.cookiePrefix,
34+
token: existingCookie.value,
35+
})
36+
37+
return { message: 'Token refreshed successfully', success: true }
4138
}

0 commit comments

Comments
 (0)