Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trying to use defineEventHandler with before[] for per-route middleware #862

Open
acidjazz opened this issue Aug 11, 2024 · 4 comments
Open
Labels
bug Something isn't working

Comments

@acidjazz
Copy link

Environment

I noticed that in this merge #485 there is before: [] - i tried something like this:

const index = defineEventHandler({
  handler: async () => {
    return metapi().render(
      await prisma.user.findMany({
      }),
    )
  },
  before: [
    () => {
      console.log('we are in before[]')
      return auth.user() && auth.user().isAdmin
    },
  ],
})

Reproduction

Just run the above code in a route

Describe the bug

not sure if i'm using before: [] incorrectly?

Additional context

No response

Logs

No response

@acidjazz acidjazz added the bug Something isn't working label Aug 11, 2024
@offizium-berndstorath
Copy link

@acidjazz
Copy link
Author

Read https://h3.unjs.io/guide/event-handler#object-syntax

Are you saying I should use onRequest: [] instead of before: [] ?

@alifnuryana
Copy link

Read https://h3.unjs.io/guide/event-handler#object-syntax

I think the documentation for defineEventHandler is incomplete.

there is no example of how to apply defineEventHandler with object syntax.

what if i want to create middleware at a non-global route level. did i make defineEventHandler in another file that accepts the object event then I called it on the body defineEventHandler by inserting an event object in the route level?

@kentntwari
Copy link

Read https://h3.unjs.io/guide/event-handler#object-syntax

I think the documentation for defineEventHandler is incomplete.

there is no example of how to apply defineEventHandler with object syntax.

what if i want to create middleware at a non-global route level. did i make defineEventHandler in another file that accepts the object event then I called it on the body defineEventHandler by inserting an event object in the route level?

true. The docs are quite cryptic to say the least.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants