TypeError: auth(...).protect is not a function

Good evening,

I get an error message saying that “protect” is not a function and in the tutorial I’m following it’s clearly indicated like that.

Do you have any idea ?

import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server';

const isPublicRoute = createRouteMatcher(['/api/uploading']);

export default clerkMiddleware((auth, request) => {
    if (!isPublicRoute(request)) {
        auth().protect();
    }
});

export const config = {
  matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};

What’s auth() returning?

You are using a newer version of Clerk. Change to

await auth.protect();

as per docs.

5 Likes

it works like that thank you

2 Likes

I still get the same error with the middleware below:

import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server';

const isProtectedRoute = createRouteMatcher([
    '/dashboard(.*)',
    '/forum(.*)'
]);

export default clerkMiddleware( async (auth, req) => {
    if(isProtectedRoute(req)) await auth().protect();
})

export const config = {
    matcher: [
    '/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',
    // Always run for API routes
    '/(api|trpc)(.*)',
    ],
};

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.