-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproxy.ts
More file actions
46 lines (40 loc) · 1.2 KB
/
proxy.ts
File metadata and controls
46 lines (40 loc) · 1.2 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
function decodeJwt(token: string | undefined) {
if (!token) return null;
try {
const parts = token.split(".");
if (parts.length < 2) return null;
const payload = parts[1].replace(/-/g, "+").replace(/_/g, "/");
const json = Buffer.from(payload, "base64").toString("utf8");
return JSON.parse(json);
} catch (e) {
return null;
}
}
export function proxy(req: NextRequest) {
const token = req.cookies.get("token")?.value;
const url = req.nextUrl.clone();
// protect dashboard routes
if (req.nextUrl.pathname.startsWith("/dashboard")) {
// if (!token) {
// url.pathname = "/login";
// return NextResponse.redirect(url);
// }
}
if (req.nextUrl.pathname.startsWith("/admin")) {
// if (!token) {
// url.pathname = "/login";
// return NextResponse.redirect(url);
// }
// const payload = decodeJwt(token);
// if (!payload || payload.role !== "admin") {
// url.pathname = "/";
// return NextResponse.redirect(url);
// }
}
return NextResponse.next();
}
export const config = {
matcher: ["/dashboard/:path*", "/admin/:path*"],
};