-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmiddleware.ts
54 lines (43 loc) · 1.78 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export const config = {
matcher: [
"/((?!api/|_next/|_static/|[\\w-]+\\.\\w+).*)",
],
};
export async function middleware(req: NextRequest) {
const url = req.nextUrl;
let hostname = req.headers.get("host") || '';
// Remove port if it exists
hostname = hostname.split(':')[0];
// Define allowed domains (including main domain and localhost)
const allowedDomains = ["tudominio.ar", "www.tudominio.ar", "localhost"];
// Check if the current hostname is in the list of allowed domains
const isMainDomain = allowedDomains.includes(hostname);
// Extract subdomain if not a main domain
const subdomain = isMainDomain ? null : hostname.split('.')[0];
console.log('Middleware: Hostname:', hostname);
console.log('Middleware: Subdomain:', subdomain);
// If it's a main domain, allow the request to proceed
if (isMainDomain) {
console.log('Middleware: Main domain detected, passing through');
return NextResponse.next();
}
// Handle subdomain logic
if (subdomain) {
try {
// Use fetch to verify if the subdomain exists
const response = await fetch(`${url.origin}/api/tenant?subdomain=${subdomain}`);
if (response.ok) {
console.log('Middleware: Valid subdomain detected, rewriting URL');
// Rewrite the URL to a dynamic route based on the subdomain
return NextResponse.rewrite(new URL(`/${subdomain}${url.pathname}`, req.url));
}
} catch (error) {
console.error('Middleware: Error fetching tenant:', error);
}
}
console.log('Middleware: Invalid subdomain or domain, returning 404');
// If none of the above conditions are met, return a 404 response
return new NextResponse(null, { status: 404 });
}