-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproxy.ts
More file actions
169 lines (158 loc) · 5.59 KB
/
proxy.ts
File metadata and controls
169 lines (158 loc) · 5.59 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
161
162
163
164
165
166
167
168
169
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server'
import { NextResponse } from 'next/server'
const isPublicRoute = createRouteMatcher([
'/',
'/sign-in(.*)',
'/marketplace(.*)',
'/campaigns(.*)',
'/welfare-programs(.*)',
'/programs(.*)',
'/donors',
'/about',
'/activities(.*)',
'/programs',
'/contribute',
'/sponsorship',
'/transparency',
'/contact',
'/thank-you(.*)',
'/thank-you',
'/donate(.*)',
'/our-services(.*)',
'/api/public(.*)',
'/api/receipts(.*)',
'/api/upload/cloudinary(.*)',
'/api/razorpay(.*)',
'/api/protected/collection-requests(.*)',
'/workflow',
'/members',
'/donors'
])
// Define protected routes and their allowed roles
// Order matters: more specific routes MUST come before broader catch-alls.
const protectedRoutes = [
// Verification page accessible to admin and moderator (placed first so it matches before general /admin)
{
matcher: createRouteMatcher(['/admin/verify-requests']),
allowedRoles: ['admin', 'moderator'],
routeName: 'Verify Requests'
},
{
matcher: createRouteMatcher(['admin/money-donations']),
allowedRoles: ['admin', 'moderator', 'accountant'],
routeName: 'Verify Requests'
},
// Main Admin Dashboard accessible to admin and moderator
{
matcher: createRouteMatcher(['/admin']),
allowedRoles: ['admin', 'moderator'],
routeName: 'Admin Dashboard'
},
// All other admin routes (catch-all). We removed the unsupported negative lookahead pattern.
{
matcher: createRouteMatcher(['/admin(.*)']),
allowedRoles: ['admin'],
routeName: 'Admin Level'
},
{
matcher: createRouteMatcher(['/admin/navigation(.*)']),
allowedRoles: ['admin', 'moderator'],
routeName: 'Admin Level'
},
{
matcher: createRouteMatcher(['/api/protected/users(.*)']),
allowedRoles: ['admin', 'moderator', 'user', 'field_executive', 'accountant', 'surveyor'],
routeName: 'User Management'
},
{
matcher: createRouteMatcher(['/moderator(.*)', '/manage(.*)']),
allowedRoles: ['admin', 'moderator'],
routeName: 'Moderator'
},
{
matcher: createRouteMatcher(['/field-executive(.*)']),
allowedRoles: ['admin', 'field_executive', 'moderator'],
routeName: 'Scrap Management'
},
{
matcher: createRouteMatcher(['/list-donation(.*)']),
allowedRoles: ['admin', 'field_executive', 'moderator'],
routeName: 'List Donation'
},
{
matcher: createRouteMatcher(['/dashboard(.*)']),
allowedRoles: ['admin', 'moderator', 'user', 'field_executive'],
routeName: 'Dashboard'
},
{
matcher: createRouteMatcher(['/surveyor(.*)', '/api/sponsorship(.*)', '/api/sponsorship/survey/draft']),
allowedRoles: ['admin', 'moderator', 'surveyor'],
routeName: 'Dashboard'
},
{
matcher: createRouteMatcher(['/admin/expenses(.*)', '/api/expenses(.*)']),
allowedRoles: ['admin', 'moderator', 'accountant'],
routeName: 'Expense Management'
},
{
matcher: createRouteMatcher(['/admin/gullak(.*)', '/api/expenses(.*)']),
allowedRoles: ['admin', 'moderator', 'accountant'],
routeName: 'Expense Management'
},
{
matcher: createRouteMatcher(['/api/cash-intake(.*)', '/cash-intake(.*)']),
allowedRoles: ['admin', 'moderator', 'accountant', 'auditor'],
routeName: 'Cash Intake'
},
{
matcher: createRouteMatcher(['/gullak-caretaker(.*)']),
allowedRoles: ['admin', 'moderator', 'neki_bank_manager', 'gullak_caretaker'],
routeName: 'Gullak Caretaker'
}
]
export default clerkMiddleware(async (auth, req) => {
const { userId } = await auth()
if (!isPublicRoute(req) && !userId) {
if (req.nextUrl.pathname.startsWith('/api/')) {
// Return JSON error for API routes instead of redirecting
return NextResponse.json(
{ error: 'Unauthorized', message: 'Authentication required' },
{ status: 401 }
)
}
// Only redirect non-API routes to sign-in
const signInUrl = new URL('/sign-in', req.url)
const res = NextResponse.redirect(signInUrl)
res.cookies.set('redirectTo', req.nextUrl.pathname + req.nextUrl.search, {
path: '/',
httpOnly: true,
})
return res
}
const userRole = ((await auth()).sessionClaims?.metadata as { role?: string } | undefined)?.role
const pathname = req.nextUrl.pathname
if (userRole === 'auditor' && pathname === '/cash-intake') {
const url = new URL('/cash-intake/list', req.url)
return NextResponse.redirect(url)
}
// Then check role-based authorization for protected routes
for (const route of protectedRoutes) {
if (route.matcher(req)) {
if (!userRole || !route.allowedRoles.includes(userRole)) {
const errorUrl = new URL('/unauthorized', req.url)
return NextResponse.redirect(errorUrl)
}
break // Exit loop once we find a matching route
}
}
},
{
authorizedParties: ['http://localhost:3000', 'http://192.168.19.223:3000', 'https://localhost:3000', 'https://khadimemillat.org', 'https://www.khadimemillat.org']
}
)
export const config = {
matcher: [
'/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',
'/(api|trpc)(.*)',
],
}