next js middleware infinite redirect
Unanswered
Giant panda posted this in #help-forum
Giant pandaOP
I am using next js middleware to protect my routes i want to protect my home page from unauthenticated users currently i have three pages in my app register , login , home pages.
how my middleware works : a user is authenticated if there is a cookie in the browser which is checked for before the user can access home page , if the user attempts to change the url of the app to the login page url and he is logged in ,he gets redirected back to home page , if the user is not logged in and attempts to change url to home page he is redirected to login page .
here is the scenario where the problem takes place : I am at the home page if i logout i get redirected to login page , if i attempt to navigate back and forth i get redirected to login page as expected because the page before the login page is the home page and i am not logged in , i log in again i am now redirected to home page if i attempt to go back i get a message
for a few seconds before i am redirected to home page as expected here is my network tab.
p.s. i have noticed that if i don't attempt to navigate back and forth after i logout the issue doesn't occur and if i just logout and then log in again everything works as expected
how my middleware works : a user is authenticated if there is a cookie in the browser which is checked for before the user can access home page , if the user attempts to change the url of the app to the login page url and he is logged in ,he gets redirected back to home page , if the user is not logged in and attempts to change url to home page he is redirected to login page .
here is the scenario where the problem takes place : I am at the home page if i logout i get redirected to login page , if i attempt to navigate back and forth i get redirected to login page as expected because the page before the login page is the home page and i am not logged in , i log in again i am now redirected to home page if i attempt to go back i get a message
too many redirects
for a few seconds before i am redirected to home page as expected here is my network tab.
p.s. i have noticed that if i don't attempt to navigate back and forth after i logout the issue doesn't occur and if i just logout and then log in again everything works as expected
export async function middleware(request: NextRequest, response:
NextResponse) {
const isUserLogged = request.cookies.get('chat_accessToken');
const pathname = request.nextUrl.pathname;
const authPaths = ['/login', '/register'];
if (
(isUserLogged && pathname == '/') ||
(isUserLogged && authPaths.includes(pathname) && pathname !== '/home/chats')
) {
return NextResponse.redirect(new URL('/home/chats', request.url));
}
if (!pathname.includes('/login') && !isUserLogged) {
return NextResponse.redirect(new URL('/login', request.url));
}
return NextResponse.next();
}
export const config = {
matcher: [
'/((?!api|_next/static|_next/image|favicon.ico).*)',
],
};
2 Replies
Giant pandaOP
network tab when this problem occurs
this gif showcases the issue described in the post