-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js.bak
More file actions
32 lines (25 loc) · 874 Bytes
/
Copy pathproxy.js.bak
File metadata and controls
32 lines (25 loc) · 874 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
import { NextResponse } from "next/server";
import { getToken } from "next-auth/jwt";
export async function proxy(req) {
const token = await getToken({
req,
secret: process.env.NEXTAUTH_SECRET,
secureCookie: process.env.NODE_ENV === "production",
});
const isLoggedIn = !!token;
const { pathname } = req.nextUrl;
const isAuthPage = pathname.startsWith("/login") || pathname.startsWith("/register");
const isProtectedPage =
pathname.startsWith("/dashboard") ||
pathname.startsWith("/history");
if (!isLoggedIn && isProtectedPage) {
return NextResponse.redirect(new URL("/login", req.nextUrl));
}
if (isLoggedIn && isAuthPage) {
return NextResponse.redirect(new URL("/dashboard", req.nextUrl));
}
return NextResponse.next();
}
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};