nuxt-authorization icon indicating copy to clipboard operation
nuxt-authorization copied to clipboard

Bouncer functions are missing types for server

Open vanekj opened this issue 1 year ago • 5 comments

Hello!

I found this module which I really like for my needs, but I ran into an issue with types of the bouncer functions on the server side. For example the authorize method. The method is auto-imported, but the code imported is for the client part of the application and I can't force it to the correct type where event is the first argument. I also tried to import it manually from the module itself but also with no luck.

The file on the screenshot is nested in the server/ directory.

Snímek obrazovky 2025-01-10 v 20 22 08

Sample recreation on StackBlitz, https://stackblitz.com/edit/github-bgkk3g6m?file=server%2Fapi%2Fhello.ts

Could you please help me on that?

Thank you

vanekj avatar Jan 10 '25 19:01 vanekj

I temporarily fixed it with patch-package module by appending Event (eg. authorizeEvent) to the bouncer functions.

nuxt-authorization+0.3.2.patch
diff --git a/node_modules/nuxt-authorization/dist/runtime/server/utils/bouncer.d.ts b/node_modules/nuxt-authorization/dist/runtime/server/utils/bouncer.d.ts
index ab46b57..585a9b2 100644
--- a/node_modules/nuxt-authorization/dist/runtime/server/utils/bouncer.d.ts
+++ b/node_modules/nuxt-authorization/dist/runtime/server/utils/bouncer.d.ts
@@ -3,12 +3,12 @@ import type { BouncerAbility, BouncerArgs } from '../../../utils/index.js';
 /**
  * Allows a user to perform an action based on their role and the data.
  */
-export declare function allows<Event extends H3Event, Ability extends BouncerAbility<any>>(event: Event, ability: Ability, ...args: BouncerArgs<Ability>): Promise<boolean>;
+export declare function allowsEvent<Event extends H3Event, Ability extends BouncerAbility<any>>(event: Event, ability: Ability, ...args: BouncerArgs<Ability>): Promise<boolean>;
 /**
  * Denies a user to perform an action based on their role and the data.
  */
-export declare function denies<Event extends H3Event, Ability extends BouncerAbility<any>>(event: Event, ability: Ability, ...args: BouncerArgs<Ability>): Promise<boolean>;
+export declare function deniesEvent<Event extends H3Event, Ability extends BouncerAbility<any>>(event: Event, ability: Ability, ...args: BouncerArgs<Ability>): Promise<boolean>;
 /**
  * Throws an error if the user is not allowed to perform an action.
  */
-export declare function authorize<Event extends H3Event, Ability extends BouncerAbility<any>>(event: Event, ability: Ability, ...args: BouncerArgs<Ability>): Promise<void>;
+export declare function authorizeEvent<Event extends H3Event, Ability extends BouncerAbility<any>>(event: Event, ability: Ability, ...args: BouncerArgs<Ability>): Promise<void>;
diff --git a/node_modules/nuxt-authorization/dist/runtime/server/utils/bouncer.js b/node_modules/nuxt-authorization/dist/runtime/server/utils/bouncer.js
index 680999c..d4fbb8d 100644
--- a/node_modules/nuxt-authorization/dist/runtime/server/utils/bouncer.js
+++ b/node_modules/nuxt-authorization/dist/runtime/server/utils/bouncer.js
@@ -1,14 +1,14 @@
 import { allows as _allows, denies as _denies, authorize as _authorize, AuthorizationError } from "../../../utils";
 import { createError } from "#imports";
-export async function allows(event, ability, ...args) {
+export async function allowsEvent(event, ability, ...args) {
   const user = await event.context.$authorization.resolveServerUser();
   return _allows(ability, user, ...args);
 }
-export async function denies(event, ability, ...args) {
+export async function deniesEvent(event, ability, ...args) {
   const user = await event.context.$authorization.resolveServerUser();
   return _denies(ability, user, ...args);
 }
-export async function authorize(event, ability, ...args) {
+export async function authorizeEvent(event, ability, ...args) {
   try {
     const user = await event.context.$authorization.resolveServerUser();
     await _authorize(ability, user, ...args);

vanekj avatar Jan 13 '25 12:01 vanekj

Hey 👋,

I'm sorry about it but I'm unable to understand why it does not work. 🙁

Barbapapazes avatar Jan 26 '25 12:01 Barbapapazes

I believe TypeScript is confused between src/runtime/utils/bouncer.ts#authorize() and src/runtime/server/utils/bouncer.ts#authorize() which signatures are not the same. Maybe this will be fixed with Nuxt v4 new directory structure.

huafu avatar Feb 10 '25 08:02 huafu

Same problem, I think @huafu is right.

I found a temporal solution importing the function with the entire path:

import { denies } from "~~/node_modules/nuxt-authorization/dist/runtime/server/utils/bouncer";

negativems avatar Feb 25 '25 15:02 negativems

Daniel could give the solution

https://github.com/nuxt/nuxt/issues/29263#issuecomment-3009000635

Barbapapazes avatar Jun 26 '25 16:06 Barbapapazes