-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
35 lines (29 loc) · 1.03 KB
/
Copy pathproxy.ts
File metadata and controls
35 lines (29 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { NextResponse, type NextRequest } from "next/server";
import { OWNER_COOKIE, verifyOwnerToken } from "@/lib/ownerAuth";
// Public routes: the owner login itself, and the client-facing editor
// (which has its own password gate in lib/clientAuth.ts).
const PUBLIC = [
/^\/login(\/|$|\?)/,
/^\/edit(\/|$)/,
/^\/api\/client(\/|$)/,
/^\/api\/owner(\/|$)/,
/^\/api\/public(\/|$)/,
];
export default async function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
if (PUBLIC.some((re) => re.test(pathname))) {
return NextResponse.next();
}
const ok = await verifyOwnerToken(req.cookies.get(OWNER_COOKIE)?.value);
if (ok) return NextResponse.next();
if (pathname.startsWith("/api")) {
return NextResponse.json({ error: "unauthorized" }, { status: 401 });
}
const url = req.nextUrl.clone();
url.pathname = "/login";
url.search = `?next=${encodeURIComponent(pathname)}`;
return NextResponse.redirect(url);
}
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
};