(req: NextRequest)
| 226 | } |
| 227 | |
| 228 | export default async function proxy(req: NextRequest) { |
| 229 | const instance = await getInstanceInfo() |
| 230 | const { pathname, search } = req.nextUrl |
| 231 | const fullhost = req.headers.get('host') |
| 232 | |
| 233 | // ------------------------------------------------------------------------- |
| 234 | // 1. Admin subdomain (multi only) → rewrite to /admin route group. |
| 235 | // Idempotent: if the path already starts with /admin (e.g. internal nav |
| 236 | // uses /admin/organizations so it works in both subdomain and path mode), |
| 237 | // don't double-prefix. |
| 238 | // ------------------------------------------------------------------------- |
| 239 | if (await isAdminSubdomain(fullhost, instance)) { |
| 240 | const target = pathname === '/admin' || pathname.startsWith('/admin/') |
| 241 | ? pathname |
| 242 | : `/admin${pathname}` |
| 243 | const response = NextResponse.rewrite(new URL(`${target}${search}`, req.url)) |
| 244 | setInstanceCookies(response, instance) |
| 245 | return response |
| 246 | } |
| 247 | |
| 248 | // ------------------------------------------------------------------------- |
| 249 | // 1b. Admin path — direct /admin access works in any tenancy mode. |
| 250 | // In single mode this is the only way to reach the admin panel; in |
| 251 | // multi mode it's an alternative to the admin.{domain} subdomain. |
| 252 | // ------------------------------------------------------------------------- |
| 253 | if (pathname === '/admin' || pathname.startsWith('/admin/')) { |
| 254 | const response = NextResponse.rewrite(new URL(`${pathname}${search}`, req.url)) |
| 255 | setInstanceCookies(response, instance) |
| 256 | return response |
| 257 | } |
| 258 | |
| 259 | // ------------------------------------------------------------------------- |
| 260 | // 1b. Admin path — direct /admin access works in any tenancy mode. |
| 261 | // In single mode this is the only way to reach the admin panel; in |
| 262 | // multi mode it's an alternative to the admin.{domain} subdomain. |
| 263 | // ------------------------------------------------------------------------- |
| 264 | if (pathname === '/admin' || pathname.startsWith('/admin/')) { |
| 265 | const response = NextResponse.rewrite(new URL(`${pathname}${search}`, req.url)) |
| 266 | setInstanceCookies(response, instance) |
| 267 | return response |
| 268 | } |
| 269 | |
| 270 | // ------------------------------------------------------------------------- |
| 271 | // 2. Standard out-of-org paths |
| 272 | // ------------------------------------------------------------------------- |
| 273 | if (pathname === '/home') { |
| 274 | return NextResponse.rewrite(new URL(`${pathname}${search}`, req.url)) |
| 275 | } |
| 276 | |
| 277 | // ------------------------------------------------------------------------- |
| 278 | // 3. Auth pages — resolve tenant for cookie context, rewrite to /auth |
| 279 | // ------------------------------------------------------------------------- |
| 280 | const authPaths = ['/login', '/signup', '/reset', '/forgot', '/verify-email'] |
| 281 | if (authPaths.includes(pathname)) { |
| 282 | const resolved = await resolveTenant(req, instance) |
| 283 | const requestHeaders = tenantRequestHeaders(req, resolved, instance) |
| 284 | const response = NextResponse.rewrite( |
| 285 | new URL(`/auth${pathname}${search}`, req.url), |
nothing calls this directly
no test coverage detected