forked from peenawat6-prog/Epayroll-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
160 lines (144 loc) · 3.74 KB
/
proxy.ts
File metadata and controls
160 lines (144 loc) · 3.74 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { NextResponse } from "next/server"
import type { NextRequest } from "next/server"
import { getToken } from "next-auth/jwt"
const PUBLIC_PATHS = [
"/login",
"/employee/login",
"/employee/register",
"/management/register",
"/shop/register",
"/sales/register",
"/forgot-password",
"/reset-password",
]
const AUTH_LANDING_PATHS = ["/login", "/employee/login"]
const PROTECTED_PATHS = [
"/dev/dashboard",
"/dashboard",
"/employees",
"/attendance",
"/payroll",
"/audit",
"/ops",
"/requests",
"/employee",
]
const EMPLOYEE_ONLY_PATHS = ["/employee"]
const DEV_ONLY_PATHS = ["/dev/dashboard"]
const BACKOFFICE_PATHS = ["/dashboard", "/employees", "/attendance", "/payroll", "/audit", "/ops"]
const REGISTER_SUBDOMAIN_PREFIX = "register."
const MANAGEMENT_SUBDOMAIN_PREFIX = "manage."
function isProtectedPath(pathname: string) {
return PROTECTED_PATHS.some(
(path) => pathname === path || pathname.startsWith(`${path}/`),
)
}
function isPublicPath(pathname: string) {
return PUBLIC_PATHS.some(
(path) => pathname === path || pathname.startsWith(`${path}/`),
)
}
function isRegisterSubdomain(hostname: string) {
return hostname.toLowerCase().startsWith(REGISTER_SUBDOMAIN_PREFIX)
}
function isManagementSubdomain(hostname: string) {
return hostname.toLowerCase().startsWith(MANAGEMENT_SUBDOMAIN_PREFIX)
}
export async function proxy(req: NextRequest) {
const { pathname } = req.nextUrl
const hostname = req.nextUrl.hostname
const token = await getToken({
req,
secret: process.env.NEXTAUTH_SECRET,
})
if (
isRegisterSubdomain(hostname) &&
!pathname.startsWith("/api") &&
pathname !== "/shop/register"
) {
return NextResponse.redirect(new URL("/shop/register", req.url))
}
if (
isManagementSubdomain(hostname) &&
!pathname.startsWith("/api") &&
pathname !== "/management/register"
) {
return NextResponse.redirect(new URL("/management/register", req.url))
}
if (!token && isProtectedPath(pathname) && !isPublicPath(pathname)) {
return NextResponse.redirect(new URL("/login", req.url))
}
if (
token &&
AUTH_LANDING_PATHS.some(
(path) => pathname === path || pathname.startsWith(`${path}/`),
)
) {
return NextResponse.redirect(
new URL(
token.role === "EMPLOYEE"
? "/employee"
: token.role === "DEV"
? "/dev/dashboard"
: "/dashboard",
req.url,
),
)
}
if (
token?.role === "DEV" &&
pathname.startsWith("/dashboard")
) {
return NextResponse.redirect(new URL("/dev/dashboard", req.url))
}
if (
token &&
token.role !== "DEV" &&
DEV_ONLY_PATHS.some(
(path) => pathname === path || pathname.startsWith(`${path}/`),
)
) {
return NextResponse.redirect(
new URL(token.role === "EMPLOYEE" ? "/employee" : "/dashboard", req.url),
)
}
if (
token?.role === "EMPLOYEE" &&
BACKOFFICE_PATHS.some(
(path) => pathname === path || pathname.startsWith(`${path}/`),
)
) {
return NextResponse.redirect(new URL("/employee", req.url))
}
if (
token &&
token.role !== "EMPLOYEE" &&
EMPLOYEE_ONLY_PATHS.some(
(path) => pathname === path || pathname.startsWith(`${path}/`),
)
) {
return NextResponse.redirect(new URL("/dashboard", req.url))
}
return NextResponse.next()
}
export const config = {
matcher: [
"/",
"/login",
"/employee/login",
"/management/register",
"/shop/register",
"/sales/register",
"/forgot-password",
"/reset-password",
"/dev/dashboard/:path*",
"/dashboard/:path*",
"/employees/:path*",
"/attendance/:path*",
"/payroll/:path*",
"/audit/:path*",
"/ops/:path*",
"/requests/:path*",
"/employee/:path*",
],
}