-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
24 lines (19 loc) · 775 Bytes
/
Copy pathproxy.ts
File metadata and controls
24 lines (19 loc) · 775 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
import { NextRequest, NextResponse } from "next/server";
export function proxy(request: NextRequest) {
const session = request.cookies.get("formdb_session")?.value;
const { pathname } = request.nextUrl;
// Guard dashboard routes.
if (pathname.startsWith("/dashboard") && !session) {
const loginUrl = new URL("/login", request.url);
loginUrl.searchParams.set("next", pathname);
return NextResponse.redirect(loginUrl);
}
// Prevent logged-in users from revisiting auth screens.
if (pathname.startsWith("/login") && session) {
return NextResponse.redirect(new URL("/dashboard", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/dashboard/:path*", "/login/:path*"],
};