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

setting nuxt route middleware in routeRules #21913

Closed
danielroe opened this issue Jul 3, 2023 · 4 comments · Fixed by #25841
Closed

setting nuxt route middleware in routeRules #21913

danielroe opened this issue Jul 3, 2023 · 4 comments · Fixed by #25841

Comments

@danielroe
Copy link
Member

Nuxt currently supports defining global route middleware (with .global. suffix) and named route middleware that can be opted-into per-route using definePageMeta.

But in some cases there are more complex patterns, where users may want to set middleware for a group of pages or apply a named middleware to the entire site. For example:

To resolve these issues, we can support a new route rule. Something like:

export default defineNuxtConfig({
  routeRules: {
    '/**': {
      routeMiddleware: 'auth',
      routeMiddleware: ['auth'],
      // both above are normalised to object syntax, which allows overriding
      // in another more specific route-rule
      routeMiddleware: {
        auth: true
      }
    },
  }
})

We're currently thinking about the best name for the rule. I think at the moment routeMiddleware makes most sense as we have defineNuxtRouteMiddleware but ideas welcome to make it clear that this is only relevant in the Vue part of your app (i.e. not applied to nitro routes).

Note: This rule will be possible once #21370 is implemented.

@pi0
Copy link
Member

pi0 commented Jul 3, 2023

I like the idea but only as also discussed in a team meeting, using route or router prefixes is really unsafe and also ambegious. Nitro at any stage might use these prefixes for server middleware and it conflicts with Nuxt and they shall remain reserved especially when adding top-level keys to route rules.

pageMiddleware, appMiddleware, and nuxtMiddleware are safer and i advice to use them. (We might be thinking to rename defineNuxtRouteMiddleware to defineNuxtMiddleware or defineNuxtPageMiddleware for consistency )

@Hebilicious
Copy link
Member

I like nuxtMiddleware. It gives a good mental model, we already have nuxtPlugin and nitroPlugin, we could go all the way and rename with nuxtMiddleware and nitroMiddleware.

@kalvenschraut
Copy link

kalvenschraut commented Jul 27, 2023

Would this cover server middleware also? Main use case is I want to by default protect all API routes so they are authenticated, but have some kind of allow list of which ones can be public.

I suppose I could do this with regex and checking the path in the middleware currently, definitely would prefer setting in the nuxt config though.

@Subin-Qreative
Copy link

Would this cover server middleware also? Main use case is I want to by default protect all API routes so they are authenticated, but have some kind of allow list of which ones can be public.

I suppose I could do this with regex and checking the path in the middleware currently, definitely would prefer setting in the nuxt config though.

yes this would be such a nice feature , im suprised it is currently not possible as a config

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants