chore: snapshot before agency split
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import type { NextRequest } from 'next/server';
|
||||
|
||||
export function middleware(request: NextRequest) {
|
||||
export async function middleware(request: NextRequest) {
|
||||
const hostname = request.headers.get('host') || '';
|
||||
const url = request.nextUrl;
|
||||
|
||||
const apiBase = process.env.API_INTERNAL_URL || 'http://backend:8080';
|
||||
|
||||
// Extrair subdomínio
|
||||
const subdomain = hostname.split('.')[0];
|
||||
|
||||
@@ -17,9 +19,8 @@ export function middleware(request: NextRequest) {
|
||||
// Se for agência ({subdomain}.localhost) - validar se existe
|
||||
if (hostname.includes('.')) {
|
||||
try {
|
||||
const res = await fetch(`http://backend:8080/api/tenant/check?subdomain=${subdomain}`);
|
||||
if (res.status === 404) {
|
||||
// Redireciona para o host base (sem subdomínio)
|
||||
const res = await fetch(`${apiBase}/api/tenant/check?subdomain=${subdomain}`);
|
||||
if (!res.ok) {
|
||||
const baseHost = hostname.split('.').slice(1).join('.') || hostname;
|
||||
const redirectUrl = new URL(url.toString());
|
||||
redirectUrl.hostname = baseHost;
|
||||
@@ -27,7 +28,11 @@ export function middleware(request: NextRequest) {
|
||||
return NextResponse.redirect(redirectUrl);
|
||||
}
|
||||
} catch (err) {
|
||||
// Em caso de erro de rede, não bloquear
|
||||
const baseHost = hostname.split('.').slice(1).join('.') || hostname;
|
||||
const redirectUrl = new URL(url.toString());
|
||||
redirectUrl.hostname = baseHost;
|
||||
redirectUrl.pathname = '/';
|
||||
return NextResponse.redirect(redirectUrl);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user