-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
46 lines (40 loc) · 1.3 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
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server"
import { NextResponse } from "next/server"
const isProtectedRoute = createRouteMatcher([
"/create(.*)",
"/podcast(.*)",
"/user(.*)",
])
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)) {
// If the user is not authenticated, redirect them to the sign-in page
const authObject = await auth()
if (!authObject.userId) {
return NextResponse.redirect(new URL("/sign-in", origin))
}
}
if (!baseHost.includes(host as string) && reqPath.includes("/podcast")) {
return fetch(`${origin}/api/domain?host=${host}`)
.then((response) => response.json())
.then((data) => {
if (data.status === 200 && data) {
return NextResponse.rewrite(
new URL(reqPath, `https://${data.domain}/${reqPath}`),
)
}
return NextResponse.next()
})
.catch((error) => {
console.error("Error fetching domain:", error)
return NextResponse.next()
})
}
return NextResponse.next()
})
export const config = {
matcher: ["/((?!.*\\..*|_next|api).*)", "/", "/podcast(.*)"],
}