Files
examjam-frontend/torpedo/middleware.ts
2025-07-03 01:43:25 +06:00

23 lines
724 B
TypeScript

import { NextResponse } from 'next/server';
import { getToken } from 'next-auth/jwt';
import type { NextRequest } from 'next/server';
export async function middleware(req: NextRequest) {
// Get the token from the request
const token = await getToken({ req, secret: process.env.NEXTAUTH_SECRET });
const { pathname } = req.nextUrl;
// If the user is not authenticated and trying to access the dashboard, redirect to login
if (!token && pathname.startsWith('/dashboard')) {
return NextResponse.redirect(new URL('/auth/login', req.url));
}
// Allow the request to proceed
return NextResponse.next();
}
export const config = {
matcher: ['/dashboard/:path*'], // Protect all routes under /dashboard
};