forked from diwenne/openreply
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
43 lines (36 loc) · 1.23 KB
/
Copy pathproxy.ts
File metadata and controls
43 lines (36 loc) · 1.23 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
import { NextResponse, type NextRequest } from "next/server";
const PROTECTED_PREFIXES = ["/dashboard", "/automations", "/logs", "/settings"];
function hasSessionCookie(request: NextRequest): boolean {
return (
request.cookies.has("authjs.session-token") ||
request.cookies.has("__Secure-authjs.session-token") ||
request.cookies.has("next-auth.session-token") ||
request.cookies.has("__Secure-next-auth.session-token")
);
}
export function proxy(request: NextRequest) {
const pathname = request.nextUrl.pathname;
const isProtected = PROTECTED_PREFIXES.some(
(prefix) => pathname === prefix || pathname.startsWith(`${prefix}/`)
);
const isLogin = pathname === "/login";
const isAuthenticated = hasSessionCookie(request);
if (isProtected && !isAuthenticated) {
const loginUrl = new URL("/login", request.url);
loginUrl.searchParams.set("callbackUrl", pathname);
return NextResponse.redirect(loginUrl);
}
if (isLogin && isAuthenticated) {
return NextResponse.redirect(new URL("/dashboard", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: [
"/dashboard/:path*",
"/automations/:path*",
"/logs/:path*",
"/settings/:path*",
"/login",
],
};