I am using Next.js version 14.1.0, and I have created a file named middleware.js in the src directory. My src folder contains the following subdirectories:
- app
- common
- components
Within the app directory, there is a subdirectory named student among others. I am trying to protect the student route using middleware.
Here is the content of my middleware.js file:
import { NextResponse } from 'next/server'
// This function can be marked `async` if using `await` inside
export function middleware(request) {
const path = request.nextUrl.pathname
const isPublicPath = path === '/login' || path === '/signup'
const token = request.cookies.get('token')?.value || ''
if(isPublicPath && token){
return NextResponse.redirect(new URL('/' , request.nextUrl))
}
if(!isPublicPath && !token){
return NextResponse.redirect(new URL('/login' , request.nextUrl))
}
}
export const config = {
matcher: '/student/:path*',
}
Here is my next.config.mjs file
/** @type {import('next').NextConfig} */
const nextConfig = {
// skipTrailingSlashRedirect: true,
// trailingSlash: true,
// output: 'export',
api: {
bodyParser: {
sizeLimit: "20mb",
},
},
images: {
domains: ['20.78.10.88'],
},
};
export default nextConfig;
Here is my jsconfig.json file
{
"compilerOptions": {
"paths": {
"@/*": ["./src/*"]
}
}
}
I suspect there might be an issue related to some missing configurations in the next.config.mjs file and jsconfig.json file. What could be the possible solution to resolve this issue?
isPublicPathandtokenwhen the page is rendered and paste them here?