-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
34 lines (26 loc) · 1.03 KB
/
Copy pathproxy.ts
File metadata and controls
34 lines (26 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
import NextAuth from "next-auth";
import { authConfig } from "@/auth.config";
import { NextResponse } from "next/server";
/**
* Edge-runtime auth gate for protected routes. Uses the slim authConfig only
* — Firestore and other Node-only deps are loaded inside server components / API routes.
*/
const { auth } = NextAuth(authConfig);
const PROTECTED_PREFIXES = ["/dashboard", "/inbox", "/research"];
export default auth((req) => {
const { pathname, search } = req.nextUrl;
if (req.nextUrl.searchParams.get("demo") === "1") {
return NextResponse.next();
}
const requiresAuth = PROTECTED_PREFIXES.some((p) => pathname.startsWith(p));
if (!requiresAuth) return NextResponse.next();
if (!req.auth) {
const signInUrl = new URL("/api/auth/signin", req.nextUrl.origin);
signInUrl.searchParams.set("callbackUrl", pathname + search);
return NextResponse.redirect(signInUrl);
}
return NextResponse.next();
});
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico|api/auth|api/cron).*)"],
};