-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
55 lines (46 loc) · 1.92 KB
/
Copy pathmiddleware.ts
File metadata and controls
55 lines (46 loc) · 1.92 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - public files (images, fonts, etc.)
*/
'/((?!api|_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
],
};
export function middleware(req: NextRequest) {
const url = req.nextUrl;
// Get the hostname (e.g., join.yourdomain.com or admin.yourdomain.com)
// We use headers.get('host') to support local testing (localhost:3000)
const hostname = req.headers.get('host') || '';
// Get the configured join domain from environment variables
// Example: NEXT_PUBLIC_JOIN_DOMAIN=join.yourchurch.com
const joinDomain = process.env.NEXT_PUBLIC_JOIN_DOMAIN;
const isJoinPortal = process.env.NEXT_PUBLIC_IS_JOIN_PORTAL === 'true';
// If this Vercel project is designated as the Public Join Portal
if (isJoinPortal || (joinDomain && hostname === joinDomain)) {
// 1. If they visit the root path "/", rewrite to "/join"
if (url.pathname === '/') {
return NextResponse.rewrite(new URL('/join', req.url));
}
// 2. Prevent access to the admin dashboard.
// If they try to access any path other than "/join", redirect them back to the root
if (url.pathname !== '/join') {
return NextResponse.redirect(new URL('/', req.url));
}
}
// Optional: If you want to block access to "/join" on the admin domain
// so people ONLY use the public domain, you can uncomment this block:
/*
if (joinDomain && hostname !== joinDomain && url.pathname === '/join') {
// Redirect them to the correct public join domain
return NextResponse.redirect(`https://${joinDomain}`);
}
*/
return NextResponse.next();
}