-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
33 lines (28 loc) · 825 Bytes
/
proxy.ts
File metadata and controls
33 lines (28 loc) · 825 Bytes
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
import { jwtVerify } from "jose";
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
function getSecret() {
const secret = process.env.AUTH_SECRET;
return new TextEncoder().encode(secret);
}
export async function proxy(req: NextRequest) {
const token = req.cookies.get("colsof_session")?.value;
if (!token) {
const url = req.nextUrl.clone();
url.pathname = "/auth";
return NextResponse.redirect(url);
}
try {
await jwtVerify(token, getSecret());
return NextResponse.next();
} catch {
const url = req.nextUrl.clone();
url.pathname = "/auth";
const res = NextResponse.redirect(url);
res.cookies.set("colsof_session", "", { path: "/", maxAge: 0 });
return res;
}
}
export const config = {
matcher: ["/dashboard/:path*"],
};