-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
40 lines (33 loc) · 1.26 KB
/
middleware.ts
File metadata and controls
40 lines (33 loc) · 1.26 KB
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
import { NextFetchEvent, NextRequest, NextResponse } from 'next/server';
import { withMiddlewareAuthRequired } from '@auth0/nextjs-auth0/edge';
export function middleware(request: NextRequest, event: NextFetchEvent) {
const url = request.nextUrl;
const hostname = request.headers.get('host') || '';
const subdomains = hostname.split('.');
const path = url.pathname;
/* Check if it's a subdomain app request.
Allow: my-app.localhost:3000 or my-app.appine.app
Disallow: xyz.my-app.localhost:3000 or my-app.appine.tech or xyz.my-app.appine.app
*/
if ((hostname.includes('localhost') && subdomains.length === 2) || (subdomains.length === 3 && subdomains[2] === 'app')) {
// Rewrite the URL to include the subdomain as a path
const subdomain = subdomains[0];
url.pathname = `/app/${subdomain}${path}`;
// Redirect to the new URL
return NextResponse.rewrite(url);
}
// Allow access to the root route without authentication
if (path === '/') {
return NextResponse.next();
}
// For non-subdomain requests (except root), apply authentication
return withMiddlewareAuthRequired()(request, event);
};
export const config = {
matcher: [
'/',
'/dashboard/:path*',
'/projects/:id/:path*',
'/settings/:path*'
],
};