forked from sayuru-akash/cca-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
53 lines (47 loc) · 1.7 KB
/
proxy.ts
File metadata and controls
53 lines (47 loc) · 1.7 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
const sessionToken =
request.cookies.get("authjs.session-token")?.value ||
request.cookies.get("__Secure-authjs.session-token")?.value;
const hasSessionCookie = !!sessionToken;
// Root path - redirect to login or dashboard based on auth status
if (pathname === "/") {
if (hasSessionCookie) {
return NextResponse.redirect(new URL("/dashboard", request.url));
}
return NextResponse.redirect(new URL("/auth/login", request.url));
}
// Protected routes
const isProtectedRoute =
pathname.startsWith("/dashboard") ||
pathname.startsWith("/programmes") ||
pathname.startsWith("/students") ||
pathname.startsWith("/audit-students") ||
pathname.startsWith("/courses") ||
pathname.startsWith("/analytics") ||
pathname.startsWith("/resources") ||
pathname.startsWith("/settings") ||
pathname.startsWith("/profile");
// Redirect to login if trying to access protected route while not authenticated
if (isProtectedRoute && !hasSessionCookie) {
const callbackUrl = encodeURIComponent(pathname + request.nextUrl.search);
return NextResponse.redirect(
new URL(`/auth/login?callbackUrl=${callbackUrl}`, request.url),
);
}
return NextResponse.next();
}
export const config = {
matcher: [
/*
* Match all request paths except:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - public files (public folder)
*/
"/((?!_next/static|_next/image|favicon.ico|.*\\..*|api).*)",
],
};