import { NextResponse, type NextRequest } from 'next/server' // The admin panel and the APIs that log in are only for people signed in // to exe.dev. On a private VM that's everyone who gets in at all; on a // public one, everyone else is sent to exe.dev's sign-in page first. // The site and the read API stay open to visitors. // From https://exe.dev/deploy/payload export function proxy(request: NextRequest) { if (request.headers.get('x-exedev-email')) return NextResponse.next() if (request.nextUrl.pathname.startsWith('/api/')) { return NextResponse.json({ errors: [{ message: 'Sign in to exe.dev first.' }] }, { status: 401 }) } // request.url says localhost; the address the visitor used is in X-Forwarded-Host. const host = request.headers.get('x-forwarded-host') ?? request.headers.get('host') const login = new URL(`https://${host}/__exe.dev/login`) login.searchParams.set('redirect', request.nextUrl.pathname + request.nextUrl.search) return NextResponse.redirect(login) } export const config = { matcher: ['/admin', '/admin/:path*', '/api/users', '/api/users/:path*', '/api/graphql', '/api/graphql-playground'], }