-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
35 lines (32 loc) · 1.06 KB
/
middleware.ts
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
import { NextRequest, NextResponse } from "next/server";
import { getToken } from "next-auth/jwt";
const secret = process.env.NEXT_AUTH_SECRET;
export async function middleware(req: NextRequest) {
const pathname = req.nextUrl.pathname;
const token = await getToken({ req, secret });
if (
token != null &&
token.role == "admin" &&
(pathname == "/hospitals" || pathname == "/patient-dashboard")
) {
return NextResponse.redirect(new URL("/admin-dashboard", req.url));
}
if (token === null)
return NextResponse.redirect(new URL("/api/auth/signin", req.url));
const date: any = Date.now();
if (
token &&
typeof token.exp === "number" &&
Date.now() >= token.exp * 1000
) {
return NextResponse.redirect(new URL("/api/auth/signin", req.url));
}
if (req.nextUrl.pathname.startsWith("/admin-dashboard")) {
if (token != null && token.role != "admin") {
return NextResponse.redirect(new URL("/admin-signup", req.url));
}
}
}
export const config = {
matcher: ["/patient-dashboard", "/admin-dashboard", "/hospitals"],
};