Branchwell

Inkspring / website / middleware.js

1import { next } from '@vercel/functions';
2export const config = { runtime: 'nodejs', matcher: '/' };
3export default function middleware(request) {
4 const url = new URL(request.url);
5 // X already registers the site root. Intercept only complete OAuth returns
6 // before the static homepage/cache; ordinary page visits remain static.
7 if (url.pathname === '/' && url.searchParams.has('state') && (url.searchParams.has('code') || url.searchParams.has('error'))) {
8 const destination = new URL('https://inkspring.ai/api/connect');
9 destination.searchParams.set('action', 'callback');
10 destination.searchParams.set('provider', 'x');
11 for (const key of ['state', 'code', 'error']) if (url.searchParams.has(key)) destination.searchParams.set(key, url.searchParams.get(key));
12 return new Response(null, { status: 302, headers: { Location: destination.href, 'Cache-Control': 'no-store', 'Referrer-Policy': 'no-referrer' } });
13 }
14 return next();
15}