Skip to content

Commit 45fd69c

Browse files
authored
chore: refactor usage of statusMessage to message; (#1039)
chore: remove redundant code in `refresh-token.server.ts`
1 parent bc52d21 commit 45fd69c

File tree

10 files changed

+22
-27
lines changed

10 files changed

+22
-27
lines changed

docs/guide/authjs/server-side/session-access.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default eventHandler(async (event) => {
4949
const session = await getServerSession(event)
5050
if (!session) {
5151
throw createError({
52-
statusMessage: 'Unauthenticated',
52+
message: 'Unauthenticated',
5353
statusCode: 403
5454
})
5555
}

docs/recipes/community/directus.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export default NuxtAuthHandler({
124124
if (!userTokens || !userTokens.data || !userDetails || !userDetails.data) {
125125
throw createError({
126126
statusCode: 500,
127-
statusMessage: 'Next auth failed',
127+
message: 'Next auth failed',
128128
})
129129
}
130130

@@ -151,7 +151,7 @@ export default NuxtAuthHandler({
151151
if (!allowedRoles.includes(user.role)) {
152152
throw createError({
153153
statusCode: 403,
154-
statusMessage: 'Not allowed',
154+
message: 'Not allowed',
155155
})
156156
}
157157

playground-authjs/server/middleware/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ export default eventHandler(async (event) => {
99

1010
const session = await getServerSession(event)
1111
if (!session) {
12-
throw createError({ statusMessage: 'Unauthenticated', statusCode: 403 })
12+
throw createError({ message: 'Unauthenticated', statusCode: 403 })
1313
}
1414
})

playground-local/server/api/auth/login.post.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default eventHandler(async (event) => {
1111
if (!result.success) {
1212
throw createError({
1313
statusCode: 403,
14-
statusMessage: 'Unauthorized, hint: try `hunter2` as password'
14+
message: 'Unauthorized, hint: try `hunter2` as password'
1515
})
1616
}
1717

playground-local/server/api/auth/refresh.post.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default eventHandler(async (event) => {
1414
if (!refreshToken || !authorizationHeader) {
1515
throw createError({
1616
statusCode: 401,
17-
statusMessage: 'Unauthorized, no refreshToken or no Authorization header'
17+
message: 'Unauthorized, no refreshToken or no Authorization header'
1818
})
1919
}
2020

@@ -23,7 +23,7 @@ export default eventHandler(async (event) => {
2323
if (!decoded) {
2424
throw createError({
2525
statusCode: 401,
26-
statusMessage: 'Unauthorized, refreshToken can\'t be verified'
26+
message: 'Unauthorized, refreshToken can\'t be verified'
2727
})
2828
}
2929

@@ -32,7 +32,7 @@ export default eventHandler(async (event) => {
3232
if (!userTokens) {
3333
throw createError({
3434
statusCode: 401,
35-
statusMessage: 'Unauthorized, user is not logged in'
35+
message: 'Unauthorized, user is not logged in'
3636
})
3737
}
3838

@@ -47,7 +47,7 @@ export default eventHandler(async (event) => {
4747
})
4848
throw createError({
4949
statusCode: 401,
50-
statusMessage: 'Tokens mismatch - this is not good'
50+
message: 'Tokens mismatch - this is not good'
5151
})
5252
}
5353

playground-local/server/api/auth/signup.post.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default eventHandler(async (event) => {
66
if (!result.success) {
77
throw createError({
88
statusCode: 400,
9-
statusMessage: `Invalid input, please provide a valid username, and a password must be 'hunter2' for this demo.`
9+
message: `Invalid input, please provide a valid username, and a password must be 'hunter2' for this demo.`
1010
})
1111
}
1212

playground-local/server/api/auth/user.get.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { JwtPayload } from '~/server/utils/session'
55
export default eventHandler((event) => {
66
const authorizationHeader = getRequestHeader(event, 'Authorization')
77
if (typeof authorizationHeader === 'undefined') {
8-
throw createError({ statusCode: 403, statusMessage: 'Need to pass valid Bearer-authorization header to access this endpoint' })
8+
throw createError({ statusCode: 403, message: 'Need to pass valid Bearer-authorization header to access this endpoint' })
99
}
1010

1111
const requestAccessToken = extractTokenFromAuthorizationHeader(authorizationHeader)
@@ -23,15 +23,15 @@ export default eventHandler((event) => {
2323
msg: 'Login failed. Here\'s the raw error:',
2424
error
2525
})
26-
throw createError({ statusCode: 403, statusMessage: 'You must be logged in to use this endpoint' })
26+
throw createError({ statusCode: 403, message: 'You must be logged in to use this endpoint' })
2727
}
2828

2929
// Get tokens of a user (only for demo, use a DB in your implementation)
3030
const userTokens = getTokensByUser(decoded.username)
3131
if (!userTokens) {
3232
throw createError({
3333
statusCode: 404,
34-
statusMessage: 'User not found'
34+
message: 'User not found'
3535
})
3636
}
3737

@@ -40,7 +40,7 @@ export default eventHandler((event) => {
4040
if (!tokensValidityCheck.valid) {
4141
throw createError({
4242
statusCode: 401,
43-
statusMessage: 'Unauthorized, user is not logged in'
43+
message: 'Unauthorized, user is not logged in'
4444
})
4545
}
4646

src/runtime/composables/authjs/useAuth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ export function useAuth(): UseAuthReturn {
286286
)
287287

288288
if (!csrfToken) {
289-
throw createError({ statusCode: 400, statusMessage: 'Could not fetch CSRF Token for signing out' })
289+
throw createError({ statusCode: 400, message: 'Could not fetch CSRF Token for signing out' })
290290
}
291291

292292
const signoutData = await _fetch<{ url: string }>(nuxt, '/signout', {

src/runtime/plugins/refresh-token.server.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import type { DeepRequired } from 'ts-essentials'
21
import { _fetch } from '../utils/fetch'
32
import { jsonPointerGet, objectFromJsonPointer, useTypedBackendConfig } from '../helpers'
4-
import type { ProviderLocal } from '../types'
53
import { defineNuxtPlugin, useAuthState, useRuntimeConfig } from '#imports'
64

75
export default defineNuxtPlugin({
@@ -12,17 +10,14 @@ export default defineNuxtPlugin({
1210
= useAuthState()
1311

1412
if (refreshToken.value && token.value) {
15-
const config = nuxtApp.$config.public.auth
16-
const configToken = useTypedBackendConfig(useRuntimeConfig(), 'local')
17-
18-
const provider = config.provider as DeepRequired<ProviderLocal>
13+
const provider = useTypedBackendConfig(useRuntimeConfig(), 'local')
1914

2015
const { path, method } = provider.refresh.endpoint
2116
const refreshRequestTokenPointer = provider.refresh.token.refreshRequestTokenPointer
2217

2318
// include header in case of auth is required to avoid 403 rejection
2419
const headers = new Headers({
25-
[configToken.token.headerName]: token.value
20+
[provider.token.headerName]: token.value
2621
} as HeadersInit)
2722

2823
try {
@@ -48,7 +43,7 @@ export default defineNuxtPlugin({
4843
}
4944

5045
// check if refreshTokenOnly
51-
if (!configToken.refresh.refreshOnlyToken) {
46+
if (!provider.refresh.refreshOnlyToken) {
5247
const extractedRefreshToken = jsonPointerGet(
5348
response,
5449
provider.refresh.token.signInResponseRefreshTokenPointer

src/runtime/server/services/authjs/nuxtAuthHandler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export async function getServerSession(event: H3Event) {
120120
// This call gives it a chance to load + initialize the variable. If it fails we still throw. This edge-case has happened to user matijao#7025 on discord.
121121
await $fetch(sessionUrlPath, { headers }).catch(error => error.data)
122122
if (!preparedAuthjsHandler) {
123-
throw createError({ statusCode: 500, statusMessage: 'Tried to get server session without setting up an endpoint to handle authentication (see https://github.com/sidebase/nuxt-auth#quick-start)' })
123+
throw createError({ statusCode: 500, message: 'Tried to get server session without setting up an endpoint to handle authentication (see https://github.com/sidebase/nuxt-auth#quick-start)' })
124124
}
125125
}
126126

@@ -205,7 +205,7 @@ async function createRequestForAuthjs(
205205
const { action, providerId } = parseActionAndProvider(event)
206206
const error = query.error
207207
if (Array.isArray(error)) {
208-
throw createError({ statusCode: 400, statusMessage: 'Error query parameter can only appear once' })
208+
throw createError({ statusCode: 400, message: 'Error query parameter can only appear once' })
209209
}
210210

211211
// Parse a body if the request method is supported, use `undefined` otherwise
@@ -235,15 +235,15 @@ function parseActionAndProvider({ context }: H3Event): { action: AuthAction, pro
235235
const params: string[] | undefined = context.params?._?.split('/')
236236

237237
if (!params || ![1, 2].includes(params.length)) {
238-
throw createError({ statusCode: 400, statusMessage: `Invalid path used for auth-endpoint. Supply either one path parameter (e.g., \`/api/auth/session\`) or two (e.g., \`/api/auth/signin/github\` after the base path (in previous examples base path was: \`/api/auth/\`. Received \`${params}\`` })
238+
throw createError({ statusCode: 400, message: `Invalid path used for auth-endpoint. Supply either one path parameter (e.g., \`/api/auth/session\`) or two (e.g., \`/api/auth/signin/github\` after the base path (in previous examples base path was: \`/api/auth/\`. Received \`${params}\`` })
239239
}
240240

241241
const [unvalidatedAction, providerId] = params
242242

243243
// Get TS to correctly infer the type of `unvalidatedAction`
244244
const action = SUPPORTED_ACTIONS.find(action => action === unvalidatedAction)
245245
if (!action) {
246-
throw createError({ statusCode: 400, statusMessage: `Called endpoint with unsupported action ${unvalidatedAction}. Only the following actions are supported: ${SUPPORTED_ACTIONS.join(', ')}` })
246+
throw createError({ statusCode: 400, message: `Called endpoint with unsupported action ${unvalidatedAction}. Only the following actions are supported: ${SUPPORTED_ACTIONS.join(', ')}` })
247247
}
248248

249249
return { action, providerId }

0 commit comments

Comments
 (0)