forked from masonfox/scurry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
34 lines (31 loc) · 1.08 KB
/
Copy pathproxy.ts
File metadata and controls
34 lines (31 loc) · 1.08 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
// Proxy to check for session cookie and redirect to /login if not present
import { NextResponse } from 'next/server';
import { SESSION_COOKIE, ALLOWED_PATHS } from './src/lib/constants.js';
export default function proxy(request) {
const { pathname, search } = request.nextUrl;
// Allow API routes and static files
if (ALLOWED_PATHS.some((p) => pathname.startsWith(p))) {
return NextResponse.next();
}
// Allow login page
if (pathname === '/login') {
return NextResponse.next();
}
const session = request.cookies.get(SESSION_COOKIE)?.value;
if (!session) {
const url = request.nextUrl.clone();
url.pathname = '/login';
// Clear existing search params first
url.search = '';
// Preserve query string by adding original URL as a redirect parameter
if (pathname !== '/' || search) {
const originalUrl = pathname + search;
url.searchParams.set('redirect', originalUrl);
}
return NextResponse.redirect(url);
}
return NextResponse.next();
}
export const config = {
matcher: ['/((?!api|_next|favicon.ico|apple-icon.png|logo.png).*)'],
};