SPB Git forge
7commits 1branches 0releases
229.0 KBsize
maindefault branch
12 days agolast push
TypeScript 91.8% HTML 3.2% JavaScript 3% SQL 1.4% CSS 0.7%
1.1 KB · 25 lines typescript
Raw Blame History
1import { NextResponse, type NextRequest } from "next/server";23/**4 * Edge gate: redirect unauthenticated visitors to /access. Token verification (HMAC) happens in the5 * Node route handlers; here we only check presence + expiry to keep the edge runtime crypto-free.6 */7export function proxy(req: NextRequest) {8  const { pathname } = req.nextUrl;9  if (pathname.startsWith("/access") || pathname.startsWith("/_next") || pathname === "/favicon.ico" || pathname.startsWith("/brand")) return NextResponse.next();10  const gate = process.env.SRC_DASHBOARD_PASSCODE;11  if (!gate) return NextResponse.next();12  const token = req.cookies.get("src_access")?.value;13  const exp = token ? Number(token.split(".")[0]) : 0;14  if (!token || !(exp > Date.now())) {15    if (pathname.startsWith("/api")) return NextResponse.json({ error: "unauthorized" }, { status: 401 });16    const url = req.nextUrl.clone();17    url.pathname = "/access";18    url.searchParams.set("next", pathname);19    return NextResponse.redirect(url);20  }21  return NextResponse.next();22}2324export const config = { matcher: ["/((?!_next/static|_next/image).*)"] };25