-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
28 lines (22 loc) · 1.01 KB
/
middleware.ts
File metadata and controls
28 lines (22 loc) · 1.01 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
import { NextRequest, NextResponse } from "next/server";
import { getSessionUser } from "./auth/session";
import { sharedPages, pages } from "./lib/pages";
// 1. Ignore certain paths
export const config = {
matcher: ["/((?!api|static|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)"],
};
export async function middleware(req: NextRequest) {
// 2. Get the currently logged in user
const user = await getSessionUser();
// 3. Redirect
if (user) {
if (sharedPages.some((page) => page.path === req.nextUrl.pathname)) return NextResponse.rewrite(new URL("/private/shared" + req.nextUrl.pathname, req.nextUrl));
let hit = null;
user.roles.forEach((role) => {
if (pages[role].some((page) => req.nextUrl.pathname.startsWith(page.path))) {
hit = NextResponse.rewrite(new URL("/private/" + role + req.nextUrl.pathname, req.nextUrl));
}
});
return hit ?? NextResponse.redirect(new URL("/", req.nextUrl));
} else return NextResponse.rewrite(new URL("/public" + req.nextUrl.pathname, req.nextUrl));
}