|
1 | | -// import { db } from "@cap/database"; |
| 1 | +import { db } from "@cap/database"; |
2 | 2 | import { organizations } from "@cap/database/schema"; |
3 | 3 | import { buildEnv, serverEnv } from "@cap/env"; |
4 | 4 | import { eq } from "drizzle-orm"; |
@@ -66,58 +66,59 @@ export async function middleware(request: NextRequest) { |
66 | 66 | try { |
67 | 67 | // We're on a custom domain at this point |
68 | 68 | // Only allow /s/ routes for custom domains |
69 | | - // if (!path.startsWith("/s/")) { |
70 | | - const url = new URL(request.url); |
71 | | - url.hostname = webUrl; |
72 | | - return NextResponse.redirect(url); |
73 | | - // } |
74 | | - |
75 | | - // // Check if we have a cached verification |
76 | | - // const verifiedDomain = request.cookies.get("verified_domain"); |
77 | | - // if (verifiedDomain?.value === hostname) return NextResponse.next(); |
78 | | - |
79 | | - // // Query the space with this custom domain |
80 | | - // const [organization] = await db() |
81 | | - // .select() |
82 | | - // .from(organizations) |
83 | | - // .where(eq(organizations.customDomain, hostname)); |
84 | | - |
85 | | - // if (!organization || !organization.domainVerified) { |
86 | | - // // If no verified custom domain found, redirect to main domain |
87 | | - // const url = new URL(request.url); |
88 | | - // url.hostname = webUrl; |
89 | | - // return NextResponse.redirect(url); |
90 | | - // } |
91 | | - |
92 | | - // // Set verification cookie for non-API routes too |
93 | | - // const response = NextResponse.next(); |
94 | | - // response.cookies.set("verified_domain", hostname, { |
95 | | - // httpOnly: true, |
96 | | - // secure: process.env.NODE_ENV === "production", |
97 | | - // sameSite: "strict", |
98 | | - // maxAge: 3600, // Cache for 1 hour |
99 | | - // }); |
100 | | - |
101 | | - // // Get the pathname and referrer |
102 | | - // const { pathname } = request.nextUrl; |
103 | | - // const referrer = request.headers.get("referer") || ""; |
104 | | - |
105 | | - // // Parse user agent with the userAgent utility |
106 | | - // const ua = userAgent(request); |
107 | | - |
108 | | - // // Add custom headers to check in generateMetadata |
109 | | - // response.headers.set("x-pathname", pathname); |
110 | | - // response.headers.set("x-referrer", referrer); |
111 | | - // response.headers.set("x-user-agent", JSON.stringify(ua)); |
112 | | - |
113 | | - // return response; |
| 69 | + if (!path.startsWith("/s/")) { |
| 70 | + const url = new URL(request.url); |
| 71 | + url.hostname = webUrl; |
| 72 | + return NextResponse.redirect(url); |
| 73 | + } |
| 74 | + |
| 75 | + // Check if we have a cached verification |
| 76 | + const verifiedDomain = request.cookies.get("verified_domain"); |
| 77 | + if (verifiedDomain?.value === hostname) return NextResponse.next(); |
| 78 | + |
| 79 | + // Query the space with this custom domain |
| 80 | + const [organization] = await db() |
| 81 | + .select() |
| 82 | + .from(organizations) |
| 83 | + .where(eq(organizations.customDomain, hostname)); |
| 84 | + |
| 85 | + if (!organization || !organization.domainVerified) { |
| 86 | + // If no verified custom domain found, redirect to main domain |
| 87 | + const url = new URL(request.url); |
| 88 | + url.hostname = webUrl; |
| 89 | + return NextResponse.redirect(url); |
| 90 | + } |
| 91 | + |
| 92 | + // Set verification cookie for non-API routes too |
| 93 | + const response = NextResponse.next(); |
| 94 | + response.cookies.set("verified_domain", hostname, { |
| 95 | + httpOnly: true, |
| 96 | + secure: process.env.NODE_ENV === "production", |
| 97 | + sameSite: "strict", |
| 98 | + maxAge: 3600, // Cache for 1 hour |
| 99 | + }); |
| 100 | + |
| 101 | + // Get the pathname and referrer |
| 102 | + const { pathname } = request.nextUrl; |
| 103 | + const referrer = request.headers.get("referer") || ""; |
| 104 | + |
| 105 | + // Parse user agent with the userAgent utility |
| 106 | + const ua = userAgent(request); |
| 107 | + |
| 108 | + // Add custom headers to check in generateMetadata |
| 109 | + response.headers.set("x-pathname", pathname); |
| 110 | + response.headers.set("x-referrer", referrer); |
| 111 | + response.headers.set("x-user-agent", JSON.stringify(ua)); |
| 112 | + |
| 113 | + return response; |
114 | 114 | } catch (error) { |
115 | 115 | console.error("Error in middleware:", error); |
116 | 116 | return notFound(); |
117 | 117 | } |
118 | 118 | } |
119 | 119 |
|
120 | 120 | export const config = { |
| 121 | + runtime: "nodejs", |
121 | 122 | matcher: [ |
122 | 123 | /* |
123 | 124 | * Match all request paths except for the ones starting with: |
|
0 commit comments