-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
70 lines (60 loc) · 2.22 KB
/
middleware.ts
File metadata and controls
70 lines (60 loc) · 2.22 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
62
63
64
65
66
67
68
69
70
// middleware.ts
import { NextRequest, NextResponse } from "next/server";
import { jwtVerify } from "jose";
const SECRET = process.env.JWT_SECRET;
async function verifyJWT(token: string) {
if (!SECRET) return null;
try {
const { payload } = await jwtVerify(token, new TextEncoder().encode(SECRET));
// payload can include userId, email, role if you signed them
return payload as { userId?: string; email?: string; role?: string };
} catch {
return null;
}
}
export async function middleware(req: NextRequest) {
const { pathname, search } = req.nextUrl;
const token = req.cookies.get("token")?.value;
const payload = token ? await verifyJWT(token) : null;
const authed = !!payload?.userId;
// ---- Protect app areas that require login ----
const isProtected =
pathname.startsWith("/student") ||
pathname.startsWith("/instructor") ||
pathname.startsWith("/admin");
if (isProtected && !authed) {
const url = new URL("/auth/signin", req.url);
url.searchParams.set("next", pathname + search);
return NextResponse.redirect(url);
}
// ---- Optional: simple role gates (uncomment if you sign `role` in JWT) ----
// if (pathname.startsWith("/student") && payload?.role && payload.role !== "student") {
// return NextResponse.redirect(new URL("/403", req.url));
// }
// if (pathname.startsWith("/instructor") && payload?.role && payload.role !== "instructor") {
// return NextResponse.redirect(new URL("/403", req.url));
// }
// if (pathname.startsWith("/admin") && payload?.role && payload.role !== "admin") {
// return NextResponse.redirect(new URL("/403", req.url));
// }
// ---- Auth routes behavior ----
if (pathname.startsWith("/auth")) {
// Always allow logout to clear cookie
if (pathname === "/auth/logout") return NextResponse.next();
// If already logged in, keep them out of signin/signup
if (authed) {
// Send them to a sensible default
return NextResponse.redirect(new URL("/student/dashboard", req.url));
}
}
return NextResponse.next();
}
export const config = {
// Only run middleware on the routes we care about
matcher: [
"/student/:path*",
"/instructor/:path*",
"/admin/:path*",
"/auth/:path*",
],
};