-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
33 lines (27 loc) · 1.14 KB
/
proxy.ts
File metadata and controls
33 lines (27 loc) · 1.14 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
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { AuthSingleton } from "@/server/auth";
let proxy_count = 0;
export async function proxy(request: NextRequest) {
console.log("I am proxy " + proxy_count++ + ", " + request.url);
// TODO: put in matcher
if (request.nextUrl.pathname == "/admin") {
let auth_token = request.cookies.get("auth_token")?.value;
if (auth_token === undefined || !(await AuthSingleton.isSessionAuthorized(auth_token))) {
const newUrl = request.nextUrl.clone();
newUrl.pathname = "/login"
return NextResponse.redirect(newUrl);
}
} else if (request.nextUrl.pathname == "/login") {
let auth_token = request.cookies.get("auth_token")?.value;
if (auth_token !== undefined && (await AuthSingleton.isSessionAuthorized(auth_token))) {
const newUrl = request.nextUrl.clone();
newUrl.pathname = "/admin"
return NextResponse.redirect(newUrl);
}
}
return NextResponse.next();
}
export const config = {
matcher: '/((?!_next/|_vercel/|favicon.ico).*)'
}