43 lines
1.0 KiB
TypeScript
43 lines
1.0 KiB
TypeScript
// middleware.ts
|
|
import { NextResponse } from "next/server";
|
|
import type { NextRequest } from "next/server";
|
|
|
|
export function middleware(req: NextRequest) {
|
|
const { pathname } = req.nextUrl;
|
|
const token = req.cookies.get("session_token")?.value;
|
|
|
|
// Protect admin API routes
|
|
if (pathname.startsWith("/api/admin")) {
|
|
if (!token) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
}
|
|
|
|
// Protect builds API routes
|
|
if (pathname.startsWith("/api/builds")) {
|
|
if (!token) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
}
|
|
|
|
// Protect admin pages
|
|
if (pathname.startsWith("/admin")) {
|
|
if (!token) {
|
|
const url = req.nextUrl.clone();
|
|
url.pathname = "/login";
|
|
url.searchParams.set("next", pathname);
|
|
return NextResponse.redirect(url);
|
|
}
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
matcher: [
|
|
"/api/admin/:path*",
|
|
"/api/builds",
|
|
"/api/builds/:path+",
|
|
"/admin/:path*",
|
|
],
|
|
}; |