-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmiddleware.ts
More file actions
33 lines (27 loc) Β· 962 Bytes
/
middleware.ts
File metadata and controls
33 lines (27 loc) Β· 962 Bytes
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
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server"
import { NextResponse } from "next/server"
const isProtectedRoute = createRouteMatcher(["/group(.*)"])
export default clerkMiddleware(async (auth, req) => {
const baseHost = "localhost:3000"
const host = req.headers.get("host")
const reqPath = req.nextUrl.pathname
const origin = req.nextUrl.origin
if (isProtectedRoute(req)) auth.protect()
if (!baseHost.includes(host as string) && reqPath.includes("/group")) {
const response = await fetch(`${origin}/api/domain?host=${host}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
})
const data = await response.json()
if (data.status === 200 && data) {
return NextResponse.rewrite(
new URL(reqPath, `https://${data.domain}/${reqPath}`),
)
}
}
})
export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
}