SPB Git
919 B · 18 lines typescript
1import { NextResponse, type NextRequest } from "next/server";23const PUBLIC = [/^\/login/, /^\/api\/auth\//, /^\/api\/health$/, /^\/manifest\.webmanifest$/, /^\/sw\.js$/, /^\/icons\//, /^\/_next\//, /^\/favicon/, /^\/offline/];4const COOKIE = process.env.NODE_ENV === "production" ? "__Host-note_session" : "note_session";56export function proxy(req: NextRequest) {7  const { pathname } = req.nextUrl;8  if (PUBLIC.some((re) => re.test(pathname))) return NextResponse.next();9  const hasCookie = Boolean(req.cookies.get(COOKIE)?.value);10  if (!hasCookie) {11    if (pathname.startsWith("/api/")) return NextResponse.json({ error: "Unauthenticated" }, { status: 401 });12    const url = req.nextUrl.clone(); url.pathname = "/login"; url.searchParams.set("next", pathname);13    return NextResponse.redirect(url);14  }15  return NextResponse.next();16}17export const config = { matcher: ["/((?!_next/static|_next/image).*)"] };18
Files · main
Menu

Theme