-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
60 lines (54 loc) · 1.54 KB
/
middleware.ts
File metadata and controls
60 lines (54 loc) · 1.54 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
import { authMiddleware } from "@clerk/nextjs";
export default authMiddleware({
publicRoutes: [
"/",
"/categories",
"/category/(.*)",
"/consultancies",
"/consultancy/(.*)",
"/about",
"/contact",
"/sign-up",
"/sign-in",
"/api/(.*)",
"/consultancy-admin",
"/consultancy-setup"
],
ignoredRoutes: [
"/api/webhook",
"/_next",
"/favicon.ico",
"/((?!api|trpc))(_next/static)(.*)",
"/((?!api|trpc))(_next/image)(.*)"
],
debug: true,
afterAuth(auth, req, evt) {
// For debugging
console.log("Auth state:", auth.isPublicRoute, auth.userId);
if (auth.userId) {
const role = auth.user?.publicMetadata?.role;
const url = req.nextUrl.pathname;
// If user is signed in and on homepage, redirect based on role
if (url === "/") {
if (role === "consultancy") {
return Response.redirect(new URL("/consultancy-dashboard", req.url));
} else if (role === "user") {
return Response.redirect(new URL("/dashboard", req.url));
}
}
// Consultancy role redirects
if (role === "consultancy") {
if (url === "/dashboard") {
return Response.redirect(new URL("/consultancy-dashboard", req.url));
}
}
// User role redirects
if (role === "user" && url === "/consultancy-dashboard") {
return Response.redirect(new URL("/dashboard", req.url));
}
}
}
});
export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};