-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
47 lines (39 loc) · 1.27 KB
/
middleware.ts
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
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
const isPrivateRoute = createRouteMatcher([
"/favorites",
"/bookings",
"/reviews",
"/dashboard(.*)",
]);
const isOwnerRoute = createRouteMatcher([
"/dashboard/rentals(.*)",
"/dashboard/reservations",
"/dashboard/messages",
]);
const isDashboardRoute = createRouteMatcher(["/dashboard"]);
const url = process.env.NEXT_PUBLIC_URL;
export default clerkMiddleware(async (auth, req) => {
const { userId } = await auth();
if (isPrivateRoute(req)) {
await auth.protect();
}
if (isDashboardRoute(req)) {
return NextResponse.redirect(new URL("/dashboard/profile", req.url));
}
if (userId && isOwnerRoute(req)) {
const urlWithParams = new URL(`${url}/api/checkUserType`);
urlWithParams.searchParams.append("userId", userId);
const res = await fetch(urlWithParams, {
method: "GET",
headers: { "Content-Type": "application/json" },
});
const { isUserAnOwner }: { isUserAnOwner: boolean } = await res.json();
if (!isUserAnOwner) {
return NextResponse.redirect(new URL("/dashboard/profile", req.url));
}
}
});
export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};