NuxtN
Nuxt3mo ago
12 replies
Yandiro

protect dynamic routes in the middleware

Hello!

I want to protect routes such as /users/[id] in one middleware file, can I do it in a simple way?

What I am doing is

export const uuidRegExp = /[0-9a-fA-F]{32}/


const routesRegExp = [
    new RegExp(`^/api/users/${uuidRegExp.source}/`)
  ]

  if (routesRegExp.some(regExp => regExp.test(event.path))) {
    await authorizeSelf(event)
  }


But I don't really like this idea and I can't use getRouterParam(event, 'id') in the middleware.

I'm looking for suggestions and ideas.

Thanks! ✌️
Was this page helpful?