I have been trying to debug this since a while now but I can’t can somwone help me with this. Here I am using next-auth the route just keeps on redirecting to dashboard causing error.
export async function middleware(request: NextRequest) {
const token=await getToken({req:request, raw:true})
console.log('middleware executing')
const url=request.nextUrl
if(token &&
(
url.pathname.startsWith('/sign-in') ||
url.pathname.startsWith('/sign-up') ||
url.pathname.startsWith('/verify') ||
url.pathname.startsWith('/')
)
){
console.log('redirecting')
return NextResponse.redirect(new URL('/dashboard',request.url))
}
if(!token && url.pathname.startsWith('/dashboard')){
return NextResponse.redirect(new URL('/sign-in',request.url))
}
return NextResponse.next()
}
Not sure what is wrong with this. In console I can see middleware executing and redirecting many times. Here is the config.
export const config = {
matcher: [
'/sign-in',
'/sign-up',
'/',
'/dashboard/:path*',
'/verify/:path*'
]
}
I tired debugging it many times. And I want it to redirect to dashboard once and not causing an error
This page isn’t workinglocalhost redirected you too many times. Try deleting your cookies. ERR_TOO_MANY_REDIRECTS