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

middleware for special routes #239

Closed
mhfeizi opened this issue May 14, 2022 · 7 comments
Closed

middleware for special routes #239

mhfeizi opened this issue May 14, 2022 · 7 comments
Labels
enhancement New feature or request h3 upstream

Comments

@mhfeizi
Copy link

mhfeizi commented May 14, 2022

Is it possible to add middleware for special routes?

@aliosmandev
Copy link

Did you find a solution for this issue?

@Intevel
Copy link
Contributor

Intevel commented Oct 18, 2022

@mhfeizi I think currently not, middlewares are called on every route.
Maybe we have to wait until they implement this feature.
@pi0 ?
See this comment: https://github.com/unjs/nitro/blob/68a8c9f564da66dbc7a8a4658fed07c99f7b4bab/src/types/handler.ts#L13

@samohub
Copy link

samohub commented Jul 21, 2023

We encountered the same issue, and created a helper function to use instead of DefineEventHandler, to more easily handle middleware per route.

import { H3Event } from 'h3'

const defineEndpoint = (callback: Function, middleware: Function[] = []) =>{
  const newCallback = async (event: H3Event) => {
    for (let i = 0; i < middleware.length; i++) {
      const middlewareFn = middleware[i];
      if (! await middlewareFn(event)) {
        throw createError({ statusCode: 401, statusMessage: "Unauthorized" });
      }
    }
    return callback(event);
  };
  
  return defineEventHandler(newCallback)
}

export {defineEndpoint}

But perhaps there are better ways to do this? @danielroe what do you think?

@Hebilicious
Copy link
Contributor

There's a h3 feature coming that will enable this unjs/h3#424

@Hebilicious Hebilicious added enhancement New feature or request upstream h3 labels Jul 22, 2023
@clopezpro
Copy link

image

@pi0
Copy link
Member

pi0 commented Aug 4, 2023

Currently, you can conditionally run middleware in some routes:

// middleware/api-auth.ts
export default eventHandler(event => {
  if (event.path.startsWith('/api')) {
    await checkAPIAuth(event)
    return 
  }
})

with the upcoming unjs/h3#485, you can also directly define hooks only on routes you want alternatively in route level:

// api/foo.ts
export default eventHandler({
  before: [checkAPIAuth],
  handler(event) {
    // 
  }
})

@clopezpro please use event.context to set a user and access it somewhere else.

@pi0 pi0 closed this as completed Aug 4, 2023
@clopezpro
Copy link

Currently, you can conditionally run middleware in some routes:

// middleware/api-auth.ts
export default eventHandler(event => {
  if (event.path.startsWith('/api')) {
    await checkAPIAuth(event)
    return 
  }
})

with the upcoming unjs/h3#485, you can also directly define hooks only on routes you want alternatively in route level:

// api/foo.ts
export default eventHandler({
  before: [checkAPIAuth],
  handler(event) {
    // 
  }
})

@clopezpro please use event.context to set a user and access it somewhere else.

magnificent, thank you very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request h3 upstream
Projects
None yet
Development

No branches or pull requests

7 participants