payment-backoffice/middleware.ts
2025-07-22 19:57:38 +02:00

31 lines
1.1 KiB
TypeScript

// middleware.ts
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(request: NextRequest) {
const token = request.cookies.get("auth_token")?.value; // Get token from cookie
// Define protected paths
const protectedPaths = ["/dashboard", "/settings", "/admin"];
const isProtected = protectedPaths.some((path) =>
request.nextUrl.pathname.startsWith(path)
);
// If accessing a protected path and no token
if (isProtected && !token) {
// Redirect to login page
const loginUrl = new URL("/login", request.url);
// Optional: Add a redirect query param to return to original page after login
loginUrl.searchParams.set("redirect", request.nextUrl.pathname);
return NextResponse.redirect(loginUrl);
}
// Allow the request to proceed if not protected or token exists
return NextResponse.next();
}
// Configure matcher to run middleware on specific paths
export const config = {
matcher: ["/dashboard/:path*", "/settings/:path*", "/admin/:path*"], // Apply to dashboard and its sub-paths
};