-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmiddleware.ts
199 lines (166 loc) · 6.37 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
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
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 } from './lib/domains'
import { handleApiDocsRoute, handleApiRoute, handleBrandDomain, handleCustomDomain, handleDoDomain, handleDoManagementDomain, handleGatewayDomain } from './lib/middleware'
import { RequestHandler } from './lib/middleware/request-handler'
/**
* 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.
* It also handles authentication using next-auth.
*/
/**
* 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
await handler.fetchCfData()
if (handler.isApiAuthRoute() || handler.isPublicRoute()) {
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 (handler.pathname === '/pricing') {
console.log('Handling /pricing special case', { hostname: handler.hostname, pathname: handler.pathname })
const targetHostname = 'functions.do' // Always use functions.do for the /pricing path
return NextResponse.rewrite(new URL(`${request.nextUrl.origin}/sites/${targetHostname}/pricing${request.nextUrl.search}`, request.url))
}
if (isDoDomain(handler.hostname) && !isGatewayDomain(handler.hostname) && !handler.pathname.startsWith('/api/') && !handler.pathname.startsWith('/v1/')) {
const doResponse = handleDoDomain(request)
if (doResponse) {
return doResponse
}
return NextResponse.next()
}
if (handler.isApiRoute()) {
console.log('Handling API route', { hostname: handler.hostname, pathname: handler.pathname, search: handler.search })
if (handler.isApiDocsRoute()) {
return handleApiDocsRoute(request)
}
const apiRouteResponse = handleApiRoute(request)
if (apiRouteResponse) {
return apiRouteResponse
}
return NextResponse.next()
}
if (isGatewayDomain(handler.hostname)) {
if (handler.pathname === '/pricing') {
console.log('Handling /pricing special case for gateway domain', { hostname: handler.hostname, pathname: handler.pathname })
const targetHostname = 'functions.do' // Always use functions.do for the /pricing path
return NextResponse.rewrite(new URL(`${request.nextUrl.origin}/sites/${targetHostname}/pricing${request.nextUrl.search}`, request.url))
}
const gatewayResponse = handleGatewayDomain(request)
if (gatewayResponse) {
return gatewayResponse
}
return NextResponse.next()
}
if (isBrandDomain(handler.hostname)) {
const brandResponse = handleBrandDomain(request)
if (brandResponse) {
return brandResponse
}
return NextResponse.next()
}
if (isDoManagementDomain(handler.hostname)) {
const managementResponse = handleDoManagementDomain(request)
if (managementResponse) {
return managementResponse
}
}
if (isDoDomain(handler.hostname)) {
const doResponse = handleDoDomain(request)
if (doResponse) {
return doResponse
}
return NextResponse.next()
}
return handleCustomDomain(request)
})
})
// export async function middleware(request: NextRequest) {
// const handler = new RequestHandler(request)
// await handler.fetchCfData()
// if (handler.isApiAuthRoute() || handler.isPublicRoute()) {
// return NextResponse.next()
// }
// if (handler.isProtectedRoute()) {
// if (!handler.isAdminRoute()) {
// const session = await auth()
// console.log('session', session)
// if (!session) {
// const signInUrl = new URL('/api/auth/signin', request.url)
// signInUrl.searchParams.set('callbackUrl', request.url)
// return NextResponse.redirect(signInUrl)
// }
// }
// }
// return analyticsMiddleware(request, async () => {
// if (handler.isApiRoute()) {
// console.log('Handling API route', { hostname: handler.hostname, pathname: handler.pathname, search: handler.search })
// if (handler.isApiDocsRoute()) {
// return handleApiDocsRoute(request)
// }
// const apiRouteResponse = handleApiRoute(request)
// if (apiRouteResponse) {
// return apiRouteResponse
// }
// return NextResponse.next()
// }
// if (isGatewayDomain(handler.hostname)) {
// const gatewayResponse = handleGatewayDomain(request)
// if (gatewayResponse) {
// return gatewayResponse
// }
// return NextResponse.next()
// }
// if (isBrandDomain(handler.hostname)) {
// const brandResponse = handleBrandDomain(request)
// if (brandResponse) {
// return brandResponse
// }
// return NextResponse.next()
// }
// if (isDoManagementDomain(handler.hostname)) {
// const managementResponse = handleDoManagementDomain(request)
// if (managementResponse) {
// return managementResponse
// }
// }
// if (isDoDomain(handler.hostname)) {
// const doResponse = handleDoDomain(request)
// if (doResponse) {
// return doResponse
// }
// return NextResponse.next()
// }
// return handleCustomDomain(request)
// })
// }
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)
*/
'/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)',
],
}