MCPcopy Create free account
hub / github.com/AnswerOverflow/AnswerOverflow / middleware

Function middleware

apps/main-site/src/middleware.ts:10–44  ·  view source on GitHub ↗
(req: NextRequest)

Source from the content-addressed store, hash-verified

8import type { NextRequest } from 'next/server';
9
10export 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
46export const config = {
47 matcher: [

Callers

nothing calls this directly

Calls 2

isOnMainSiteFunction · 0.90
makeMainSiteLinkFunction · 0.90

Tested by

no test coverage detected