github clerk/javascript @clerk/nuxt@1.1.0

latest releases: @clerk/types@4.92.0, @clerk/clerk-js@5.99.0, @clerk/fastify@2.4.37...
9 months ago

Minor Changes

  • Add createRouteMatcher() helper function that allows you to protect multiple pages or API routes. (#5050) by @wobsoriano

    For protecting pages (in a global route middleware):

    // createRouteMatcher is automatically imported
    const isProtectedRoute = createRouteMatcher(['/dashboard(.*)', '/forum(.*)']);
    
    export default defineNuxtRouteMiddleware(to => {
      const { userId } = useAuth();
    
      if (!userId.value && isProtectedRoute(to)) {
        // Add custom logic to run before redirecting
        return navigateTo('/sign-in');
      }
    });

    For protecting API routes:

    import { clerkMiddleware, createRouteMatcher } from '@clerk/nuxt/server';
    
    // Unlike pages, you need to import `createRouteMatcher` from `@clerk/nuxt/server`
    const isProtectedRoute = createRouteMatcher(['/api/user(.*)', '/api/projects(.*)']);
    
    export default clerkMiddleware(event => {
      const { userId } = event.context.auth;
    
      if (!userId && isProtectedRoute(event)) {
        setResponseStatus(event, 401);
        return 'You are not authorized to access this resource.';
      }
    });

Patch Changes

Don't miss a new javascript release

NewReleases is sending notifications on new releases.