(req: NextRequest)
| 8 | import type { NextRequest } from 'next/server'; |
| 9 | |
| 10 | export function middleware(req: NextRequest) { |
| 11 | const url = req.nextUrl; |
| 12 | const path = url.pathname + url.search; |
| 13 | |
| 14 | const host = req.headers.get('host')!; |
| 15 | if (path.startsWith('/og') || path.startsWith('/ingest')) { |
| 16 | return NextResponse.next(); |
| 17 | } |
| 18 | |
| 19 | if (isOnMainSite(host) || path.includes('/discord')) { |
| 20 | const authedRoutes = ['/dashboard']; |
| 21 | const authToken = req.cookies.get(AuthEdge.getNextAuthCookieName()); |
| 22 | if (authedRoutes.some((route) => path.startsWith(route))) { |
| 23 | if (!authToken) { |
| 24 | return NextResponse.redirect(makeMainSiteLink('/api/auth/signin')); |
| 25 | } |
| 26 | } |
| 27 | if (path.startsWith('/m/')) { |
| 28 | if (authToken) { |
| 29 | return NextResponse.rewrite(makeMainSiteLink(`${path}/dynamic`)); |
| 30 | } |
| 31 | } |
| 32 | return NextResponse.next(); |
| 33 | } |
| 34 | // rewrite everything else to `/[domain]/[path] dynamic route |
| 35 | const tenantAuthToken = req.cookies.get(AuthEdge.getTenantCookieName()); |
| 36 | let pathPostFix = ''; |
| 37 | if (path.startsWith('/m/')) { |
| 38 | if (tenantAuthToken) { |
| 39 | pathPostFix += '/dynamic'; |
| 40 | } |
| 41 | } |
| 42 | const newUrl = new URL(`/${host}${path}${pathPostFix}`, req.url); |
| 43 | return NextResponse.rewrite(newUrl); |
| 44 | } |
| 45 | // See "Matching Paths" below to learn more |
| 46 | export const config = { |
| 47 | matcher: [ |
nothing calls this directly
no test coverage detected