-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
33 lines (24 loc) · 868 Bytes
/
proxy.ts
File metadata and controls
33 lines (24 loc) · 868 Bytes
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
import { getToken } from "next-auth/jwt";
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
export async function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
const token = await getToken({
req: request,
secret: process.env.NEXTAUTH_SECRET,
});
const isLoginPage = pathname === "/login";
const isDemo = request.cookies.get("pharmchat-demo")?.value === "1";
if (!token && !isDemo && !isLoginPage) {
const loginUrl = new URL("/login", request.url);
return NextResponse.redirect(loginUrl);
}
if ((token || isDemo) && isLoginPage) {
const homeUrl = new URL("/", request.url);
return NextResponse.redirect(homeUrl);
}
return NextResponse.next();
}
export const config = {
matcher: ["/((?!api/auth|_next/static|_next/image|favicon.ico|logo.png).*)"],
};