41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import type { NextRequest } from "next/server";
|
|
import { AUTH_COOKIE_NAME } from "@/lib/auth";
|
|
|
|
const protectedPrefixes = [
|
|
"/dashboard",
|
|
"/customers",
|
|
"/locations",
|
|
"/contacts",
|
|
"/devices",
|
|
"/equipment",
|
|
"/validations",
|
|
"/users",
|
|
"/profile"
|
|
];
|
|
|
|
export function middleware(request: NextRequest) {
|
|
const token = request.cookies.get(AUTH_COOKIE_NAME)?.value;
|
|
const isProtected = protectedPrefixes.some(
|
|
(prefix) => request.nextUrl.pathname === prefix || request.nextUrl.pathname.startsWith(`${prefix}/`)
|
|
);
|
|
|
|
if (!token) {
|
|
if (isProtected) {
|
|
return NextResponse.redirect(new URL("/login", request.url));
|
|
}
|
|
return NextResponse.next();
|
|
}
|
|
|
|
const isLogin = request.nextUrl.pathname === "/login";
|
|
|
|
if (isLogin) {
|
|
return NextResponse.redirect(new URL("/dashboard", request.url));
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ["/dashboard/:path*", "/customers/:path*", "/locations/:path*", "/contacts/:path*", "/devices/:path*", "/equipment/:path*", "/validations/:path*", "/users/:path*", "/profile/:path*", "/login"]
|
|
};
|