middleware.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. import { NextResponse, type NextRequest } from 'next/server';
  2. const BRIVEN_API_ORIGIN = process.env.NEXT_PUBLIC_BRIVEN_API_ORIGIN ?? 'https://api.briven.tech';
  3. const BRIVEN_PROJECT_ID = process.env.NEXT_PUBLIC_BRIVEN_PROJECT_ID!;
  4. const BRIVEN_AUTH_KEY =
  5. process.env.BRIVEN_AUTH_PUBLIC_KEY ?? process.env.NEXT_PUBLIC_BRIVEN_AUTH_KEY!;
  6. export async function middleware(req: NextRequest) {
  7. if (!req.nextUrl.pathname.startsWith('/api/auth/')) return NextResponse.next();
  8. const url = new URL(
  9. req.nextUrl.pathname.replace('/api/auth', '/v1/auth-tenant'),
  10. BRIVEN_API_ORIGIN,
  11. );
  12. url.search = req.nextUrl.search;
  13. const headers = new Headers(req.headers);
  14. headers.set('x-briven-project-id', BRIVEN_PROJECT_ID);
  15. headers.set('authorization', `Bearer ${BRIVEN_AUTH_KEY}`);
  16. return fetch(url, {
  17. method: req.method,
  18. headers,
  19. body: req.body,
  20. // @ts-expect-error — duplex required for streaming bodies in Node 18+
  21. duplex: 'half',
  22. });
  23. }
  24. export const config = {
  25. matcher: ['/api/auth/:path*'],
  26. };