-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
26 lines (20 loc) · 818 Bytes
/
proxy.ts
File metadata and controls
26 lines (20 loc) · 818 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
import { NextRequest, NextResponse } from "next/server";
const AUTH_COOKIE = "devhire_session";
const protectedRoutes = ["/dashboard", "/developers", "/shortlist"];
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
const hasSession = request.cookies.get(AUTH_COOKIE)?.value === "1";
const isProtected = protectedRoutes.some(
(route) => pathname === route || pathname.startsWith(`${route}/`),
);
if (isProtected && !hasSession) {
return NextResponse.redirect(new URL("/login", request.url));
}
if (pathname === "/login" && hasSession) {
return NextResponse.redirect(new URL("/dashboard", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/dashboard/:path*", "/developers/:path*", "/shortlist/:path*", "/login"],
};