-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
49 lines (41 loc) · 1.46 KB
/
Copy pathproxy.ts
File metadata and controls
49 lines (41 loc) · 1.46 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
import { NextRequest, NextResponse } from "next/server";
import { publicRoutes, protectedRoutes } from "./lib/pathroute";
import { auth } from "@/auth";
import { libroles } from "./common/types";
export default async function proxy(req: NextRequest) {
const session = await auth();
const pathname = req.nextUrl.pathname;
const isProtected = protectedRoutes.some((route) =>
pathname.startsWith(route)
);
const isPublic = publicRoutes.includes(pathname);
if (isProtected && !session?.user?.email) {
return NextResponse.redirect(new URL("/login", req.nextUrl));
}
if (
isPublic &&
session?.user?.email &&
!req.nextUrl.pathname.startsWith("/home")
) {
return NextResponse.redirect(new URL("/home", req.nextUrl));
}
if (session?.user?.email && pathname.startsWith("/library")) {
const libid = pathname.split("/")[2];
const isadminRoute= pathname.includes(`/library/${libid}/admin`);
const check = session?.user?.libdetails.find(
(item: libroles) => item.libid === libid );
if (!check) {
return NextResponse.redirect(new URL("/unauthorized?reason=Invalid Library Id ", req.nextUrl));
}
if (
check &&
check.role != "ADMIN" && isadminRoute
){
return NextResponse.redirect(new URL("/unauthorized?reason=only admin can access this route", req.nextUrl));
}
}
return NextResponse.next();
}
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};