chore(release): snapshot 1.4.2
This commit is contained in:
@@ -11,12 +11,8 @@ export async function middleware(request: NextRequest) {
|
||||
const hostnameWithoutPort = hostname.split(':')[0];
|
||||
const subdomain = hostnameWithoutPort.split('.')[0];
|
||||
|
||||
// Rotas públicas que não precisam de validação de tenant
|
||||
const publicPaths = ['/login', '/cadastro', '/'];
|
||||
const isPublicPath = publicPaths.some(path => url.pathname === path || url.pathname.startsWith(path + '/'));
|
||||
|
||||
// Validar subdomínio de agência ({subdomain}.localhost) apenas se não for rota pública
|
||||
if (hostname.includes('.') && !isPublicPath) {
|
||||
// Se tem subdomínio (ex: vivo.localhost), SEMPRE validar se existe
|
||||
if (hostname.includes('.')) {
|
||||
try {
|
||||
const res = await fetch(`${apiBase}/api/tenant/check?subdomain=${subdomain}`, {
|
||||
cache: 'no-store',
|
||||
@@ -26,26 +22,25 @@ export async function middleware(request: NextRequest) {
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
console.error(`Tenant check failed for ${subdomain}: ${res.status}`);
|
||||
// Se for 404, realmente não existe. Se for 500, pode ser erro temporário.
|
||||
// Por segurança, vamos redirecionar apenas se tivermos certeza que falhou a validação (ex: 404)
|
||||
// ou se o backend estiver inalcançável de forma persistente.
|
||||
// Para evitar loops durante desenvolvimento, vamos permitir passar se for erro de servidor (5xx)
|
||||
// mas redirecionar se for 404.
|
||||
console.error(`❌ Tenant check failed for ${subdomain}: ${res.status}`);
|
||||
|
||||
if (res.status === 404) {
|
||||
const baseHost = hostname.split('.').slice(1).join('.') || hostname;
|
||||
const redirectUrl = new URL(url.toString());
|
||||
redirectUrl.hostname = baseHost;
|
||||
redirectUrl.pathname = '/';
|
||||
console.error(`❌ Tenant ${subdomain} não encontrado - BLOQUEANDO ACESSO`);
|
||||
// Tenant não existe, redirecionar para página principal (sem subdomínio)
|
||||
const baseHost = hostname.split('.').slice(1).join('.') || 'localhost';
|
||||
const redirectUrl = new URL(`http://${baseHost}/`);
|
||||
return NextResponse.redirect(redirectUrl);
|
||||
}
|
||||
}
|
||||
|
||||
// Se passou pela validação, tenant existe - continuar
|
||||
console.log(`✅ Tenant ${subdomain} validado com sucesso`);
|
||||
} catch (err) {
|
||||
console.error('Middleware error:', err);
|
||||
// Em caso de erro de rede (backend fora do ar), permitir carregar a página
|
||||
// para não travar o frontend completamente (pode mostrar erro na tela depois)
|
||||
// return NextResponse.next();
|
||||
console.error('❌ Middleware error:', err);
|
||||
// Em caso de erro de rede, bloquear por segurança
|
||||
const baseHost = hostname.split('.').slice(1).join('.') || 'localhost';
|
||||
const redirectUrl = new URL(`http://${baseHost}/`);
|
||||
return NextResponse.redirect(redirectUrl);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user