-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
57 lines (46 loc) · 2.02 KB
/
Copy pathmiddleware.ts
File metadata and controls
57 lines (46 loc) · 2.02 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
56
57
import { NextRequest, NextResponse } from 'next/server';
const PHOTOGRAPHER_COOKIE = 'admin_session';
const PLATFORM_ADMIN_COOKIE = 'platform_admin_session';
function photographerValue() {
return process.env.ADMIN_SESSION_SECRET ?? 'dev-secret';
}
function platformAdminValue() {
return `${process.env.ADMIN_SESSION_SECRET ?? 'dev-secret'}-admin`;
}
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
const isPhotographer = request.cookies.get(PHOTOGRAPHER_COOKIE)?.value === photographerValue();
const isPlatformAdmin = request.cookies.get(PLATFORM_ADMIN_COOKIE)?.value === platformAdminValue();
// Admin login page — redirect away if already authenticated
if (pathname === '/admin') {
if (isPlatformAdmin) return NextResponse.redirect(new URL('/admin/dashboard', request.url));
if (isPhotographer) return NextResponse.redirect(new URL('/photographer', request.url));
return NextResponse.next();
}
// Admin dashboard routes — require platform admin session
if (pathname.startsWith('/admin/dashboard')) {
if (!isPlatformAdmin) {
if (isPhotographer) return NextResponse.redirect(new URL('/photographer', request.url));
return NextResponse.redirect(new URL('/admin', request.url));
}
return NextResponse.next();
}
// Admin API routes — require platform admin session (except the auth endpoint itself)
if (pathname.startsWith('/api/admin/') && pathname !== '/api/admin/auth') {
if (!isPlatformAdmin) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
return NextResponse.next();
}
// Photographer routes — redirect platform admin to their own dashboard
if (pathname.startsWith('/photographer')) {
if (isPlatformAdmin && !isPhotographer) {
return NextResponse.redirect(new URL('/admin/dashboard', request.url));
}
return NextResponse.next();
}
return NextResponse.next();
}
export const config = {
matcher: ['/admin/:path*', '/photographer/:path*', '/api/admin/:path*'],
};