-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
57 lines (47 loc) · 1.57 KB
/
proxy.ts
File metadata and controls
57 lines (47 loc) · 1.57 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function proxy(req: NextRequest) {
const token = req.cookies.get("token")?.value;
const userRole = req.cookies.get("user_role")?.value;
const { pathname } = req.nextUrl;
const adminPaths = [
"/admin-profile",
"/admin-dashboard",
"/manage-agents",
"/manage-companies",
"/screening",
];
const agentPaths = ["/profile", "/dashboard", "/history", "/tickets"];
const isAdminRoute = adminPaths.some((path) => pathname.startsWith(path));
const isAgentRoute = agentPaths.some((path) => pathname.startsWith(path));
// 1. Sem token -> Login
if ((isAdminRoute || isAgentRoute) && !token) {
return NextResponse.redirect(new URL("/login", req.url));
}
// 2. Bloqueia AGENT de entrar em rotas ADMIN
if (isAdminRoute && userRole !== "ADMIN") {
// Se ele for um AGENT ou não tiver role, manda para a página dele
const fallback = userRole === "AGENT" ? "/profile" : "/login";
return NextResponse.redirect(new URL(fallback, req.url));
}
// 3. Bloqueia ADMIN de entrar em rotas AGENT
if (isAgentRoute && userRole === "ADMIN") {
return NextResponse.redirect(new URL("/admin-profile", req.url));
}
return NextResponse.next();
}
export const config = {
matcher: [
//admin
"/admin-profile/:path*",
"/admin-dashboard/:path*",
"/manage-agents/:path*",
"/manage-companies/:path*",
"/screening/:path*",
//agent
"/profile/:path*",
"/dashboard/:path*",
"/history/:path*",
"/tickets/:path*",
],
};