Skip to content

Commit 57345df

Browse files
committed
refactor!: prefer event.context instead of nitro-app
1 parent 2b17a4d commit 57345df

File tree

5 files changed

+13
-31
lines changed

5 files changed

+13
-31
lines changed

playground/server/nitro.d.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import type { User } from '~/types/user'
22

33
export default defineNitroPlugin((nitroApp) => {
4-
nitroApp.$authorization = {
5-
resolveServerUser: () => {
6-
return { id: 2, name: 'User 1' } satisfies User
7-
},
8-
}
4+
// https://github.com/nuxt/nuxt/issues/25710#issuecomment-1935622896
5+
nitroApp.hooks.hook('request', async (event) => {
6+
event.context.$authorization = {
7+
resolveServerUser: () => {
8+
return { id: 1, name: 'User 1' } satisfies User
9+
},
10+
}
11+
})
912
})

playground/utils/abilities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ export const listProducts = defineAbility({ allowGuest: true }, () => true)
66
export const createProduct = defineAbility(() => true)
77

88
export const editProduct = defineAbility((user: User, product: Product) => {
9+
console.log('user', user, product)
910
return user.id === product.ownerId
1011
})

src/runtime/server/nitro.d.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/runtime/server/utils/bouncer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { type H3Event } from 'h3'
22
import type { AuthorizerResponse, BouncerAbility } from '../../../utils'
33
import { allows as _allows, denies as _denies, authorize as _authorize, AuthorizationError } from '../../../utils'
4-
import { useNitroApp, createError } from '#imports'
4+
import { createError } from '#imports'
55

66
/**
77
* Allows a user to perform an action based on their role and the data.
88
*/
99
export async function allows<Ability extends BouncerAbility<any>>(event: H3Event, bouncerAbility: Ability, ...args: Ability extends { original: (user: any, ...args: infer Args) => AuthorizerResponse } ? Args : never): Promise<boolean> {
10-
const user = await useNitroApp().$authorization.resolveServerUser(event)
10+
const user = await event.context.$authorization.resolveServerUser()
1111

1212
return _allows(bouncerAbility, user, ...args)
1313
}
@@ -16,7 +16,7 @@ export async function allows<Ability extends BouncerAbility<any>>(event: H3Event
1616
* Denies a user to perform an action based on their role and the data.
1717
*/
1818
export async function denies<Ability extends BouncerAbility<any>>(event: H3Event, bouncerAbility: Ability, ...args: Ability extends { original: (user: any, ...args: infer Args) => AuthorizerResponse } ? Args : never): Promise<boolean> {
19-
const user = await useNitroApp().$authorization.resolveServerUser(event)
19+
const user = await event.context.$authorization.resolveServerUser()
2020

2121
return _denies(bouncerAbility, user, ...args)
2222
}
@@ -26,7 +26,7 @@ export async function denies<Ability extends BouncerAbility<any>>(event: H3Event
2626
*/
2727
export async function authorize<Ability extends BouncerAbility<any>>(event: H3Event, bouncerAbility: Ability, ...args: Ability extends { original: (user: any, ...args: infer Args) => AuthorizerResponse } ? Args : never): Promise<void> {
2828
try {
29-
const user = await useNitroApp().$authorization.resolveServerUser(event)
29+
const user = await event.context.$authorization.resolveServerUser()
3030

3131
await _authorize(bouncerAbility, user, ...args)
3232
}

0 commit comments

Comments
 (0)