Files
nextjs/middleware.tsx
2024-12-16 13:30:44 +05:30

28 lines
771 B
TypeScript

import { NextRequest, NextResponse } from "next/server";
export function middleware(req: NextRequest) {
const isAuth = req.cookies.get("isAuth");
console.log(isAuth);
if (!isAuth) {
console.log("No token found, redirecting to login");
return NextResponse.redirect(new URL("/login", req.url));
}
try {
// jwt.verify(token, process.env.JWT_SECRET);
console.log("Token is valid, proceeding to the dashboard");
return NextResponse.next();
} catch (error) {
console.log("Invalid token, redirecting to login");
return NextResponse.redirect(new URL("/login", req.url));
}
}
// You can apply this middleware to all pages or specific ones
export const config = {
matcher: ["/","/dashboard/:path*"], // Apply to certain paths
};