-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
51 lines (42 loc) · 1.23 KB
/
Copy pathmiddleware.ts
File metadata and controls
51 lines (42 loc) · 1.23 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
// middleware.ts
import { NextRequest, NextResponse } from 'next/server';
const PUBLIC_ASSET =
/\.(?:avif|css|eot|gif|ico|jpe?g|js|map|mp3|mp4|ogg|png|svg|ttf|webmanifest|webm|webp|woff2?)$/i;
const publicRoutes = new Set([
'/',
'/login',
'/about',
'/privacy',
'/terms',
'/robots.txt',
'/sitemap.xml',
'/llms.txt',
]);
export function middleware(request: NextRequest) {
const { pathname, search } = request.nextUrl;
if (
pathname.startsWith('/_next/') ||
pathname.startsWith('/static/') ||
PUBLIC_ASSET.test(pathname)
) {
return NextResponse.next();
}
if (publicRoutes.has(pathname)) {
return NextResponse.next();
}
// Cookie presence only — protected APIs verify Privy tokens server-side.
const token = request.cookies.get('privy-token');
if (!token) {
const loginUrl = new URL('/login', request.url);
const redirectAfter =
pathname.startsWith('/') && !pathname.startsWith('//')
? `${pathname}${search}`
: '/dashboard';
loginUrl.searchParams.set('redirect_after', redirectAfter);
return NextResponse.redirect(loginUrl);
}
return NextResponse.next();
}
export const config = {
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
};