24 lines
776 B
TypeScript
24 lines
776 B
TypeScript
import { NextResponse } from "next/server";
|
|
import type { NextRequest } from "next/server";
|
|
|
|
const COOKIE = "sam_pruebas_alumno";
|
|
// Rutas que no requieren sesión.
|
|
const PUBLICAS = ["/login"];
|
|
|
|
export function middleware(req: NextRequest) {
|
|
const { pathname } = req.nextUrl;
|
|
if (pathname.startsWith("/api/")) return NextResponse.next();
|
|
if (PUBLICAS.some((p) => pathname === p || pathname.startsWith(p + "/"))) return NextResponse.next();
|
|
if (!req.cookies.get(COOKIE)) {
|
|
const url = req.nextUrl.clone();
|
|
url.pathname = "/login";
|
|
url.searchParams.set("next", pathname);
|
|
return NextResponse.redirect(url);
|
|
}
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ["/((?!_next/static|_next/image|favicon.png|logo-sam.png|.*\\.svg).*)"]
|
|
};
|