-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmiddleware.ts
More file actions
408 lines (349 loc) · 16.9 KB
/
Copy pathmiddleware.ts
File metadata and controls
408 lines (349 loc) · 16.9 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import NextAuth from 'next-auth'
import { NextResponse } from 'next/server.js'
import { analyticsMiddleware } from './analytics/src/middleware'
import authConfig from './auth.config'
import { isBrandDomain, isDoDomain, isDoManagementDomain, isGatewayDomain, extractApiNameFromDomain, extractApiNameFromManagementDomain, isSiteDomain } from './lib/domains'
import { RequestHandler } from './lib/middleware/request-handler'
import { collectionSlugs } from './collections/slugs'
/**
* Middleware Configuration
* =======================
*
* This middleware handles routing logic for the .do domain ecosystem and custom domains.
* It determines whether requests should be routed to API endpoints or website content,
* and handles authentication using next-auth.
*
* 1. Authentication Logic
* ----------------------
* - Uses NextAuth for authentication via the auth.config.ts configuration
* - Public routes (defined in lib/routes.ts) bypass authentication checks:
* - /login, /sign-up, /api/users/me, etc.
* - Routes starting with /api/auth/ (API_AUTH_PREFIX)
* - Routes with favicon paths
* - Protected routes redirect unauthenticated users to sign-in page:
* - /dashboard, /account, /admin, /chat, etc. (defined in lib/routes.ts)
* - Redirects to /api/auth/signin with the original URL as callbackUrl
* - Admin routes have special handling with temporary auth bypass (ENG-751):
* - Currently admin routes (/admin/*) bypass authentication redirects
* - This is a temporary modification until integrated auth is restored
*
* 2. Domain Type Routing
* ---------------------
* The middleware identifies and routes different domain types:
*
* a) .do Domains:
* - Domains ending with .do, .do.gt, or .do.mw (checked via isDoDomain())
* - Non-API routes are rewritten to /sites/{hostname}{pathname}
* - API routes (/api/* or /v1/*) are rewritten to /{apiName}{path}
* - Admin routes are passed through or rewritten to specific collections
* - Example: functions.do/about → /sites/functions.do/about
*
* b) Gateway Domains:
* - Domains like apis.do, do.gt, do.mw (checked via isGatewayDomain())
* - /docs path is rewritten to /docs
* - /sites path is rewritten to /sites
* - Root path for do.gt/do.mw is rewritten to /sites
* - /pricing path is always rewritten to functions.do/pricing
*
* c) .do.management Domains:
* - Domains ending with .do.management (checked via isDoManagementDomain())
* - Rewritten to /admin or /admin/collections/{managementApiName}
* - Example: functions.do.management → /admin/collections/functions
*
* d) Brand Domains:
* - Custom domains configured in domains.config.js (checked via isBrandDomain())
* - Docs, admin, and API paths are passed through
* - Site domains are rewritten to /sites/{hostname}{pathname}
* - Root path is rewritten to /sites
* - Other paths are rewritten to /sites/.do{pathname}
*
* e) Custom Domains:
* - Any domain not matching the above categories
* - Rewritten to /projects/{hostname}{pathname}
*
* 3. Path-Based Routing
* --------------------
* The middleware handles specific path patterns:
*
* a) API Routes:
* - Paths starting with /api/ or /v1/
* - For .do domains: rewritten to /{apiName}{path}
* - For other domains: passed through
*
* b) API Documentation Routes:
* - Paths starting with /api/docs/ or /v1/docs/
* - Rewritten to docs.apis.do with modified headers
*
* c) Documentation Routes:
* - Paths starting with /docs/
* - For documentation.do: ensures path starts with /docs
* - For other domains: passed through
*
* d) Admin Routes:
* - Paths starting with /admin/
* - For .do domains with matching collection: rewritten to /admin/collections/{apiName}
* - For other cases: passed through
*
* 4. Path Duplication Prevention
* -----------------------------
* The middleware prevents path duplication when the domain is included in the path:
*
* - Checks if pathname starts with the domain name (e.g., /gpt.do/chat/new)
* - Removes the domain prefix to prevent duplication
* - Example: /gpt.do/chat/new → /sites/gpt.do/chat/new (not /sites/gpt.do/gpt.do/chat/new)
*
* 5. Special Cases
* ---------------
* The middleware handles several special cases:
*
* a) documentation.do Domain:
* - Ensures all paths start with /docs
* - Example: documentation.do/guide → /docs/guide
*
* b) /pricing Path:
* - Always rewrites to functions.do/pricing regardless of domain
* - Example: apis.do/pricing → /sites/functions.do/pricing
*
* c) Analytics Middleware:
* - All requests pass through analyticsMiddleware for tracking
*/
/**
* Main middleware function
* Handles routing logic for all incoming requests
*/
const { auth } = NextAuth(authConfig)
export default auth(async (request) => {
const handler = new RequestHandler(request)
const isLoggedIn = !!request.auth
if (handler.isApiAuthRoute() || handler.isPublicRoute() || handler.pathname.startsWith('/api/') || handler.pathname.startsWith('/v1/')) {
return NextResponse.next()
}
// TEMPORARY CHANGE (ENG-751): Admin routes bypass authentication redirects
// This is a temporary modification until integrated auth is restored
if (handler.isProtectedRoute()) {
if (!handler.isAdminRoute()) {
if (!isLoggedIn) {
const signInUrl = new URL('/api/auth/signin', request.url)
signInUrl.searchParams.set('callbackUrl', request.url)
return NextResponse.redirect(signInUrl)
}
}
}
return analyticsMiddleware(request, async () => {
if (isDoDomain(handler.hostname) && !isGatewayDomain(handler.hostname) && !handler.pathname.startsWith('/api/') && !handler.pathname.startsWith('/v1/')) {
console.log('Handling .do domain non-API route', { hostname: handler.hostname, pathname: handler.pathname })
const hostname = process.env.HOSTNAME_OVERRIDE || request.nextUrl.hostname
const { pathname, search } = request.nextUrl
const url = new URL(request.url)
if (hostname === 'documentation.do') {
console.log('Handling documentation.do domain', { pathname, search })
const pathWithDocs = pathname.startsWith('/docs') ? pathname : `/docs${pathname}`
return NextResponse.rewrite(new URL(`${url.origin}${pathWithDocs}${search}`, url))
}
const apiName = extractApiNameFromDomain(hostname)
const isAdminRoute = pathname === '/admin' || pathname.startsWith('/admin/')
if (isAdminRoute) {
console.log('Handling admin path for .do domain', { hostname, pathname, search })
if (apiName && collectionSlugs.includes(apiName)) {
console.log('Rewriting to admin collection', { hostname, pathname, search, collection: apiName })
return NextResponse.rewrite(new URL(`/admin/collections/${apiName}${search}`, url))
}
console.log('Passing through admin path for .do domain', { hostname, pathname, search })
return NextResponse.next()
}
const response = NextResponse.next()
response.headers.set('x-pathname', pathname)
if (pathname === '/docs' || pathname.startsWith('/docs/')) {
console.log('Handling docs path', { hostname, pathname, search })
return NextResponse.next()
}
if (pathname === '/api' || pathname === '/v1') {
console.log('Rewriting /api or /v1 to API root', { apiName, hostname, pathname, search })
const path = pathname === '/api' ? pathname.replace('/api', '') : pathname.replace('/v1', '')
return NextResponse.rewrite(new URL(`${url.origin}/${apiName}${path}${search}`, url))
}
// Fix path duplication by checking if pathname already includes the hostname
const domainPrefix = `/${hostname}`
let cleanPathname = pathname
// If the pathname starts with the domain name (e.g., /gpt.do/chat/new)
if (pathname.startsWith(domainPrefix)) {
// Remove the domain prefix to prevent duplication
cleanPathname = pathname.substring(domainPrefix.length)
console.log('Removed domain prefix from pathname', {
hostname,
originalPathname: pathname,
cleanPathname,
})
}
const newUrl = `${url.origin}/sites/${hostname}${cleanPathname}${search}`
console.log('Rewriting to site', { hostname, pathname: cleanPathname, search, url, newUrl })
return NextResponse.rewrite(new URL(newUrl, url))
}
if (handler.isApiRoute()) {
console.log('Handling API route', { hostname: handler.hostname, pathname: handler.pathname, search: handler.search })
console.log('Processing regular API route', { hostname: handler.hostname, pathname: handler.pathname })
const hostname = process.env.HOSTNAME_OVERRIDE || request.nextUrl.hostname
const { pathname, search } = request.nextUrl
const url = new URL(request.url)
if (isDoDomain(hostname)) {
const apiName = extractApiNameFromDomain(hostname)
console.log('Rewriting /api or /v1 to API root for .do domain', { apiName, hostname, pathname, search })
const path = pathname.startsWith('/api') ? pathname.replace('/api', '') : pathname.replace('/v1', '')
return NextResponse.rewrite(new URL(`${url.origin}/${apiName}${path}${search}`))
}
console.log('Passing through API request', { hostname, pathname, search })
return NextResponse.next()
}
if (isGatewayDomain(handler.hostname)) {
console.log('Handling gateway domain', { hostname: handler.hostname, pathname: handler.pathname })
const hostname = process.env.HOSTNAME_OVERRIDE || request.nextUrl.hostname
const { pathname, search } = request.nextUrl
const url = new URL(request.url)
console.log('Handling gateway domain, exiting middleware', {
hostname,
pathname,
search,
})
if (pathname === '/docs') {
console.log('Rewriting gateway domain /docs to docs', {
hostname,
pathname,
search,
})
return NextResponse.rewrite(new URL(`/docs${search}`, url))
}
if (pathname === '/sites') {
console.log('Rewriting gateway domain /sites to sites', {
hostname,
pathname,
search,
})
return NextResponse.rewrite(new URL(`/sites${search}`, url))
}
if ((hostname === 'do.gt' || hostname === 'do.mw') && pathname === '/') {
console.log('Rewriting do.gt/do.mw root to /sites', {
hostname,
pathname,
search,
})
return NextResponse.rewrite(new URL(`/sites${search}`, url))
}
return NextResponse.next()
}
if (isBrandDomain(handler.hostname)) {
console.log('Handling brand domain', { hostname: handler.hostname, pathname: handler.pathname })
const hostname = process.env.HOSTNAME_OVERRIDE || request.nextUrl.hostname
const { pathname, search } = request.nextUrl
const url = new URL(request.url)
console.log('Handling brand domain', { hostname, pathname, search })
if (pathname === '/docs' || pathname.startsWith('/docs/')) {
console.log('Passing through docs path for brand domain', { hostname, pathname, search })
return NextResponse.next()
}
if (pathname === '/admin' || pathname.startsWith('/admin/')) {
console.log('Passing through admin path for brand domain', { hostname, pathname, search })
return NextResponse.next()
}
if (pathname === '/api' || pathname.startsWith('/api/') || pathname === '/v1' || pathname.startsWith('/v1/')) {
console.log('Passing through API path for brand domain', { hostname, pathname, search })
return NextResponse.next()
}
if (isSiteDomain(hostname)) {
console.log(`Rewriting site domain ${hostname} to /sites/${hostname}`, { pathname, search })
return NextResponse.rewrite(new URL(`/sites/${hostname}${pathname === '/' ? '' : pathname}${search}`, url))
}
if (pathname === '/') {
console.log('Rewriting brand domain root path to /sites', { hostname, pathname, search })
return NextResponse.rewrite(new URL(`/sites${search}`, url))
}
const cleanPathname = pathname.endsWith('/') && pathname !== '/' ? pathname.slice(0, -1) : pathname
console.log('Rewriting brand domain to sites domain path using .do', { hostname, cleanPathname, search })
return NextResponse.rewrite(new URL(`/sites/.do${cleanPathname}${search}`, url))
}
if (isDoManagementDomain(handler.hostname)) {
console.log('Handling do management domain', { hostname: handler.hostname, pathname: handler.pathname })
const hostname = process.env.HOSTNAME_OVERRIDE || request.nextUrl.hostname
const { pathname, search } = request.nextUrl
const url = new URL(request.url)
const managementApiName = extractApiNameFromManagementDomain(hostname)
if (managementApiName === '') {
console.log('Skipping rewrite from do.management to /admin', { hostname, pathname, search })
return NextResponse.next()
} else {
if (managementApiName && collectionSlugs.includes(managementApiName)) {
console.log('Rewriting to admin collection', { hostname, pathname, search, collection: managementApiName })
return NextResponse.rewrite(new URL(`/admin/collections/${managementApiName}${pathname}${search}`, url))
}
console.log('Rewriting to admin path', { hostname, pathname, search })
return NextResponse.rewrite(new URL(`/admin${pathname}${search}`, url))
}
}
if (isDoDomain(handler.hostname)) {
console.log('Handling .do domain fallback', { hostname: handler.hostname, pathname: handler.pathname })
const hostname = process.env.HOSTNAME_OVERRIDE || request.nextUrl.hostname
const { pathname, search } = request.nextUrl
const url = new URL(request.url)
if (hostname === 'documentation.do') {
console.log('Handling documentation.do domain', { pathname, search })
const pathWithDocs = pathname.startsWith('/docs') ? pathname : `/docs${pathname}`
return NextResponse.rewrite(new URL(`${url.origin}${pathWithDocs}${search}`, url))
}
const apiName = extractApiNameFromDomain(hostname)
const isAdminRoute = pathname === '/admin' || pathname.startsWith('/admin/')
if (isAdminRoute) {
console.log('Handling admin path for .do domain', { hostname, pathname, search })
if (apiName && collectionSlugs.includes(apiName)) {
console.log('Rewriting to admin collection', { hostname, pathname, search, collection: apiName })
return NextResponse.rewrite(new URL(`/admin/collections/${apiName}${search}`, url))
}
console.log('Passing through admin path for .do domain', { hostname, pathname, search })
return NextResponse.next()
}
const response = NextResponse.next()
response.headers.set('x-pathname', pathname)
if (pathname === '/docs' || pathname.startsWith('/docs/')) {
console.log('Handling docs path', { hostname, pathname, search })
return NextResponse.next()
}
if (pathname === '/api' || pathname === '/v1') {
console.log('Rewriting /api or /v1 to API root', { apiName, hostname, pathname, search })
const path = pathname === '/api' ? pathname.replace('/api', '') : pathname.replace('/v1', '')
return NextResponse.rewrite(new URL(`${url.origin}/${apiName}${path}${search}`, url))
}
// Fix path duplication by checking if pathname already includes the hostname
const domainPrefix = `/${hostname}`
let cleanPathname = pathname
// If the pathname starts with the domain name (e.g., /gpt.do/chat/new)
if (pathname.startsWith(domainPrefix)) {
// Remove the domain prefix to prevent duplication
cleanPathname = pathname.substring(domainPrefix.length)
console.log('Removed domain prefix from pathname', {
hostname,
originalPathname: pathname,
cleanPathname,
})
}
console.log('Rewriting to site', { hostname, pathname: cleanPathname, search, url })
return NextResponse.rewrite(new URL(`${url.origin}/sites/${hostname}${cleanPathname}${search}`, url))
}
console.log('Handling custom domain', { hostname: handler.hostname, pathname: handler.pathname })
const hostname = process.env.HOSTNAME_OVERRIDE || request.nextUrl.hostname
const { pathname, search } = request.nextUrl
const url = new URL(request.url)
console.log('Handling custom domain', { hostname, pathname, search })
return NextResponse.rewrite(new URL(`/projects/${hostname}${pathname}${search}`, url))
})
})
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico, sitemap.xml, robots.txt (metadata files)
* - /api/ (API routes)
* - /v1/ (API routes)
*/
'/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt|api/|v1/).*)',
],
}