Next.js Discord

Discord Forum

Getting client pathname in middleware

Answered
ncls. posted this in #help-forum
Open in Discord
Hi, I'm currently doing some special protecting routes stuff.
When the user enters the /signin page, the middleware should check wether they are already authenticated or not. However, req.nextUrl.pathname does not return /signin I expected, but rather /_next/static/chunks/app/(auth)/signin/page.js which is really uncomfortable to work with and unpractical.

Is there a way I can get the route the user is seeing in their browser?
Answered by European sprat
but if you are excluding signin in the matcher... of course it's not going to run lol
View full answer

6 Replies

European sprat
You sure it doesn't show the path after that? You likely want to use the matcher to exclude static files from being handled in middleware to avoid seeing them
@European sprat You sure it doesn't show the path after that? You likely want to use the matcher to exclude static files from being handled in middleware to avoid seeing them
I am using matcher (which should actually cause the middleware not to run at all on /signin LMAO but it does):
export const config = {
  matcher: [ '/((?!api/auth|signin|assets).*)' ],
}

Also what do you mean with "show the path after that"?
I looked through the req.nextUrl object but couldn't find anything that matched what I need
European sprat
i mean it shows the hit on the static asset but then after that does it not show the hit on the path you want to check?
European sprat
but if you are excluding signin in the matcher... of course it's not going to run lol
Answer