i am using next.js version 14.1.0
, i had made a file name middleware.js
in src
directory i have the following sub directories in my src folder
- app
- common
- components
in the app
directory i have a folder which is student
and many more. i was trying to protect the student
route with middleware.
here is 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;
my jsconfig.json
file
{
"compilerOptions": {
"paths": {
"@/*": ["./src/*"]
}
}
}
i think there might be issue related to some missing configurations in the next.config.js
file and jsconfing.json
file. what could be the possible solution to resolve the issue
Umar Raza is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.