Skip to content

Commit

Permalink
refactor!: prefer event.context instead of nitro-app
Browse files Browse the repository at this point in the history
  • Loading branch information
Barbapapazes committed Aug 29, 2024
1 parent 2b17a4d commit 57345df
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 31 deletions.
11 changes: 0 additions & 11 deletions playground/server/nitro.d.ts

This file was deleted.

13 changes: 8 additions & 5 deletions playground/server/plugins/authorization-reolver.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { User } from '~/types/user'

export default defineNitroPlugin((nitroApp) => {
nitroApp.$authorization = {
resolveServerUser: () => {
return { id: 2, name: 'User 1' } satisfies User
},
}
// https://github.com/nuxt/nuxt/issues/25710#issuecomment-1935622896
nitroApp.hooks.hook('request', async (event) => {
event.context.$authorization = {
resolveServerUser: () => {
return { id: 1, name: 'User 1' } satisfies User
},
}
})
})
1 change: 1 addition & 0 deletions playground/utils/abilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export const listProducts = defineAbility({ allowGuest: true }, () => true)
export const createProduct = defineAbility(() => true)

export const editProduct = defineAbility((user: User, product: Product) => {
console.log('user', user, product)
return user.id === product.ownerId
})
11 changes: 0 additions & 11 deletions src/runtime/server/nitro.d.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/runtime/server/utils/bouncer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { type H3Event } from 'h3'
import type { AuthorizerResponse, BouncerAbility } from '../../../utils'
import { allows as _allows, denies as _denies, authorize as _authorize, AuthorizationError } from '../../../utils'
import { useNitroApp, createError } from '#imports'
import { createError } from '#imports'

/**
* Allows a user to perform an action based on their role and the data.
*/
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> {
const user = await useNitroApp().$authorization.resolveServerUser(event)
const user = await event.context.$authorization.resolveServerUser()

return _allows(bouncerAbility, user, ...args)
}
Expand All @@ -16,7 +16,7 @@ export async function allows<Ability extends BouncerAbility<any>>(event: H3Event
* Denies a user to perform an action based on their role and the data.
*/
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> {
const user = await useNitroApp().$authorization.resolveServerUser(event)
const user = await event.context.$authorization.resolveServerUser()

return _denies(bouncerAbility, user, ...args)
}
Expand All @@ -26,7 +26,7 @@ export async function denies<Ability extends BouncerAbility<any>>(event: H3Event
*/
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> {
try {
const user = await useNitroApp().$authorization.resolveServerUser(event)
const user = await event.context.$authorization.resolveServerUser()

await _authorize(bouncerAbility, user, ...args)
}
Expand Down

0 comments on commit 57345df

Please sign in to comment.