-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
50 lines (42 loc) · 1.27 KB
/
middleware.ts
File metadata and controls
50 lines (42 loc) · 1.27 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
50
import { NextRequest, NextResponse } from "next/server";
import { decode } from "next-auth/jwt";
import getSessionToken from "./app/_utils/getSessionToken";
const middleware = async (request: NextRequest) => {
const { pathname } = request.nextUrl;
const sessionToken = await getSessionToken();
if (
!sessionToken &&
!pathname.endsWith("/") &&
!pathname.endsWith("/join") &&
!pathname.endsWith("/login")
) {
return NextResponse.redirect(new URL("/", request.url));
}
try {
const decodedToken = await decode({
token: sessionToken,
secret: process.env.NEXTAUTH_SECRET as string,
});
if (
decodedToken?.sub?.startsWith("ViewerMSP") &&
pathname.startsWith("/registration")
) {
return NextResponse.redirect(new URL("/", request.url));
}
if (
decodedToken?.sub?.startsWith("RegistryMSP") &&
(pathname.startsWith("/issue") || pathname.startsWith("/history"))
) {
return NextResponse.redirect(new URL("/", request.url));
}
} catch (error) {
console.error(error);
// return NextResponse.redirect(new URL("/", request.url));
}
};
export default middleware;
export const config = {
matcher: [
"/((?!api|_next|_vercel|favicon.ico|sitemap.xml|robots.txt|.*\\..*).*)",
],
};