-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
170 lines (131 loc) · 5.81 KB
/
Copy pathmiddleware.ts
File metadata and controls
170 lines (131 loc) · 5.81 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
170
import { clerkMiddleware } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
import { authRoutes, publicRoutes, orgnaizationRoutes } from "./utils";
import { UserRole } from "./lib/generated/prisma";
export default clerkMiddleware(async (auth, req) => {
const pathname = req.nextUrl.pathname;
console.log('Incoming pathname in middleware', pathname)
// Skip auth/role checks for these API routes because they are either public,
// used by third-party services, or need to be accessible without a signed-in user.
// This prevents the middleware from redirecting or blocking legitimate requests.
if (
pathname.startsWith("/api/hotspots") ||
pathname.startsWith("/api/batches") ||
pathname.startsWith("/api/verify") ||
pathname.startsWith("/api/geminiTranslation") ||
pathname.startsWith("/api/auth")
) {
return NextResponse.next();
}
const { userId, sessionClaims } = await auth();
// ✅ Enhanced debugging for production issues
console.log("=== MIDDLEWARE DEBUG START ===");
console.log("userId:", userId);
console.log("sessionClaims exists:", !!sessionClaims);
console.log("sessionClaims.publicMetadata:", JSON.stringify(sessionClaims?.publicMetadata, null, 2));
console.log("Full sessionClaims:", JSON.stringify(sessionClaims, null, 2));
// ✅ Extract user role & organization type from metadata
type PublicMetadata = {
role?: string;
organizationType?: string;
[key: string]: unknown;
};
const publicMetadata = sessionClaims?.publicMetadata as
| PublicMetadata
| undefined;
let role = publicMetadata?.role;
let orgType = publicMetadata?.organizationType;
console.log("middleware code: role FROM CLERK", role);
console.log("middleware code: orgType FROM CLERK", orgType);
// Fallback to cookie if metadata is missing
if (!role || !orgType) {
console.log("Missing role or orgType from Clerk, checking cookie fallback...");
const cookie = req.cookies.get("user_fallback");
if (cookie) {
try {
const { role: cRole, organizationType: cOrg } = JSON.parse(
cookie.value
);
console.log("Cookie fallback found - role:", cRole, "orgType:", cOrg);
role = cRole;
orgType = cOrg;
} catch (error) {
console.log("Error parsing cookie fallback:", error);
}
} else {
console.log("No cookie fallback available");
}
}
// If still no role/orgType and user is signed in, try to fetch from database
if (userId && (!role || !orgType)) {
console.log("Still missing metadata, user might be team member needing database lookup");
// We'll handle this case in the dashboard routing logic below
}
console.log("Final values - role:", role, "orgType:", orgType);
console.log("=== MIDDLEWARE DEBUG END ===");
console.log("Middleware invoked for path:", pathname);
// ✅ Public pages that do NOT require authentication
const publicPaths = Object.values(publicRoutes);
const authPaths = Object.values(authRoutes);
if (publicPaths.some((path) => pathname.startsWith("/verify/batchUnit/"))) {
return NextResponse.next();
}
if (publicPaths.some((path) => pathname.startsWith("/verify/batch/"))) {
if (role !== UserRole.ORGANIZATION_MEMBER) {
return NextResponse.redirect(new URL(publicRoutes.unauthorized, req.url));
}
return NextResponse.next();
}
// ✅ If route is EXACTLY public or auth route, allow
if (publicPaths.includes(pathname) || authPaths.includes(pathname)) {
return NextResponse.next();
}
// ✅ Allow home page ("/") ONLY if user is not logged in
if (pathname === publicRoutes.home && !userId) {
return NextResponse.next();
}
// ✅ If user is NOT signed in, redirect to login (for protected routes)
if (!userId) {
return NextResponse.redirect(new URL(authRoutes.login, req.url));
}
// ✅ Consumer routes → only for consumers
if (pathname.startsWith("/consumer") && role !== UserRole.CONSUMER) {
return NextResponse.redirect(new URL(publicRoutes.unauthorized, req.url));
}
// ✅ Organization routes → only for organization members
if (pathname.startsWith("/dashboard")) {
console.log("Dashboard route detected, checking authorization...");
if (role !== UserRole.ORGANIZATION_MEMBER) {
console.log("User role is not ORGANIZATION_MEMBER, redirecting to unauthorized");
return NextResponse.redirect(new URL(publicRoutes.unauthorized, req.url));
}
// ✅ Check allowed dashboard based on org type
const allowedRoute = orgType
? orgnaizationRoutes[orgType.toLowerCase()]
: undefined;
console.log("Organization type:", orgType);
console.log("Allowed route:", allowedRoute);
console.log("Current pathname:", pathname);
if (!allowedRoute) {
console.log("No allowed route found for organization type:", orgType);
// If we have a signed-in org member but no orgType, redirect to a fallback route
// This can happen during magic link auth where metadata isn't immediately available
if (userId && role === UserRole.ORGANIZATION_MEMBER) {
console.log("Org member without orgType detected - redirecting to general dashboard");
return NextResponse.redirect(new URL("/dashboard", req.url));
}
return NextResponse.redirect(new URL(publicRoutes.unauthorized, req.url));
}
if (!pathname.startsWith(allowedRoute)) {
console.log("Pathname doesn't match allowed route, redirecting to correct dashboard");
return NextResponse.redirect(new URL(allowedRoute, req.url));
}
console.log("Dashboard authorization successful");
}
return NextResponse.next();
});
export const config = {
matcher: [
"/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)|api/auth|api/register|api/public|monitoring).*)",
],
};