-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
61 lines (47 loc) · 1.93 KB
/
proxy.ts
File metadata and controls
61 lines (47 loc) · 1.93 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
58
59
60
61
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
// Routes that require authentication
const protectedRoutes = ["/dashboard", "/teacher", "/lessons"];
// Routes only for teachers
const teacherRoutes = ["/teacher"];
// Routes that should redirect if already authenticated
const authRoutes = ["/auth/login", "/auth/signup"];
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
const authToken = request.cookies.get("xano_auth_token")?.value;
// Reject obviously malicious or malformed requests with extreme path lengths
const MAX_PATH_LENGTH = 2000;
const MAX_SEGMENT_LENGTH = 200;
if (pathname.length > MAX_PATH_LENGTH) {
return new NextResponse("Path too long", { status: 414 });
}
const segments = pathname.split("/").filter(Boolean);
if (segments.some((s) => s.length > MAX_SEGMENT_LENGTH)) {
return new NextResponse("Path segment too long", { status: 414 });
}
// Check if route is protected
const isProtectedRoute = protectedRoutes.some((route) =>
pathname.startsWith(route)
);
// Check if it's an auth route
const isAuthRoute = authRoutes.some((route) => pathname.startsWith(route));
// Redirect to login if accessing protected route without auth
if (isProtectedRoute && !authToken) {
const loginUrl = new URL("/auth/login", request.url);
loginUrl.searchParams.set("redirect", pathname);
return NextResponse.redirect(loginUrl);
}
// Redirect to dashboard if accessing auth routes while logged in
if (isAuthRoute && authToken) {
return NextResponse.redirect(new URL("/dashboard", request.url));
}
// For teacher routes, we can't check role in middleware (requires API call)
// Role check is done on the page level instead
return NextResponse.next();
}
export const config = {
matcher: [
// Match all routes except static files and api
"/((?!_next/static|_next/image|favicon.ico|api|.*\\.).*)",
],
};