-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
55 lines (45 loc) · 1.6 KB
/
proxy.ts
File metadata and controls
55 lines (45 loc) · 1.6 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { auth } from "@/lib/auth";
import { NextResponse } from "next/server";
export default auth((req) => {
const { nextUrl } = req;
const isLoggedIn = !!req.auth;
const isDemo = req.cookies.get("springa-demo")?.value === "1";
// Logged-in user with stale demo cookie — clear it and proceed normally
if (isDemo && isLoggedIn) {
const response = NextResponse.next();
response.cookies.delete("springa-demo");
return response;
}
// Demo mode: rewrite API routes to the demo catch-all
if (isDemo && nextUrl.pathname.startsWith("/api/") && !nextUrl.pathname.startsWith("/api/auth")) {
const demoPath = nextUrl.pathname.replace(/^\/api\//, "/api/demo/");
const url = nextUrl.clone();
url.pathname = demoPath;
return NextResponse.rewrite(url);
}
// Demo users skip auth redirects — they get data from fixtures
if (isDemo) {
return NextResponse.next();
}
// Auth API must always pass through
if (nextUrl.pathname.startsWith("/api/auth")) {
return NextResponse.next();
}
// /demo must always pass through so the route handler can set the cookie
if (nextUrl.pathname === "/demo") {
return NextResponse.next();
}
if (nextUrl.pathname === "/login") {
if (isLoggedIn) {
return NextResponse.redirect(new URL("/", nextUrl));
}
return NextResponse.next();
}
if (!isLoggedIn) {
return NextResponse.redirect(new URL("/login", nextUrl));
}
return NextResponse.next();
});
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico|icon.*\\.png|icon\\.svg|apple-icon\\.png|manifest\\.webmanifest|sw\\.js).*)"],
};