Next.js Discord

Discord Forum

migrate from authmiddleware to clerkmiddleware of clerk

Answered
rkstlohchab posted this in #help-forum
Open in Discord
@"use php"
i tried updating my code and now its not working but old code of authmiddleware is working

import { authMiddleware } from "@clerk/nextjs";
 
export default authMiddleware({
  publicRoutes: [
    '/',
    '/events/:id',
    '/api/webhook/clerk',
    '/api/webhook/stripe',
    '/api/uploadthing'
  ],
  ignoredRoutes: [
    '/api/webhook/clerk',
    '/api/webhook/stripe',
    '/api/uploadthing'
  ]
});
 
export const config = {
  matcher: ['/((?!.+\\.[\\w]+$|_next).*)', '/', '/(api|trpc)(.*)'],
};


i tried to change the above code to the following code below

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

const isPublicRoute = createRouteMatcher([
  "/", 
  "/events/:id",
  "/api/webhooks/clerk",
  "/api/webhooks/stripe",
  "/api/uploadthing",
]);

const isIgnoredRoute = createRouteMatcher([
  "/api/webhooks/clerk",
  "/api/webhooks/stripe",
  "/api/uploadthing",
]);


export default clerkMiddleware((auth, req) => {
  if (isIgnoredRoute(req)) {
    return; // Completely bypass Clerk for ignored routes
  }
  
  if (!isPublicRoute(req)) {
    auth().protect();
  }
});

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

when i go to /sign-in page it doesnt work
Answered by "use php"
Alright, I'll soon tell a helper
View full answer

52 Replies

I’m check when I get free
@"use php" I’m check when I get free
okay buddy
Check the output of isIgnoredRoute. Also verify that middleware is runing
?
yes
middleware is not working
at the start
When you open the middleware function
console.log
like this?
yes
if no console.log, stop nextjs app, delete .next folder, and try running again
@"use php"
If nothing is logged, then issue is with matcher, I believe
i did according to the docs
@rkstlohchab i did according to the docs
I’m talking about config
Where you export config
can u suggest something that seems off
I already specified,
at
export const config

Middleware doesn't seem to be invoking
@rkstlohchab Click to see attachment
have a look at this
@rkstlohchab maybe
What route are you trying to access?
/sign-up
Ya, you are getting this error because /sign-up is not in the matcher
but if thats the case
lemme try
is it correct
If you get the console.log, then yes
there needs to be a / before sign-in
hey it worked
by
adding this to the public route

"/sign-in(.*)",
  "/sign-up(.*)",
i dont get the option to mark as soln
can u show me how it is shown in your discord
I'll send a screen recording soonn
i always has to ask to mark it as solution
Original message was deleted
see the photo here
Not showing
Tried refreshing too
Alright, I'll soon tell a helper
Answer