-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
59 lines (49 loc) · 1.74 KB
/
middleware.ts
File metadata and controls
59 lines (49 loc) · 1.74 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
const PROTECTED_PREFIXES = ["/parent", "/teacher", "/admin", "/poc"];
const PUBLIC_ROUTES = ["/login", "/signup"];
const PUBLIC_API_PREFIX = "/api/auth";
const ROLE_DASHBOARD: Record<string, string> = {
parent: "/parent/dashboard",
teacher: "/teacher/dashboard",
admin: "/admin/dashboard",
};
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Always allow public API routes through
if (pathname.startsWith(PUBLIC_API_PREFIX)) {
return NextResponse.next();
}
const sessionCookie = request.cookies.get("__session")?.value;
const roleCookie = request.cookies.get("__role")?.value;
// If user is authenticated and hits / or /login, redirect to their dashboard
if (sessionCookie && (pathname === "/" || PUBLIC_ROUTES.includes(pathname))) {
const dashboard =
roleCookie && ROLE_DASHBOARD[roleCookie]
? ROLE_DASHBOARD[roleCookie]
: "/parent/dashboard";
return NextResponse.redirect(new URL(dashboard, request.url));
}
// Check if the route is protected
const isProtected = PROTECTED_PREFIXES.some((prefix) =>
pathname.startsWith(prefix)
);
if (isProtected && !sessionCookie) {
const loginUrl = new URL("/login", request.url);
loginUrl.searchParams.set("from", pathname);
return NextResponse.redirect(loginUrl);
}
return NextResponse.next();
}
export const config = {
matcher: [
/*
* Match all request paths except:
* - _next/static (static files)
* - _next/image (image optimization)
* - favicon.ico
* - public folder files
*/
"/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
],
};