-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
42 lines (35 loc) · 1.23 KB
/
middleware.ts
File metadata and controls
42 lines (35 loc) · 1.23 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
import { withAuth } from "next-auth/middleware";
import { NextResponse } from "next/server";
export default withAuth(
function middleware(req) {
const token = req.nextauth.token;
const status = token?.status;
const role = token?.role;
const { pathname } = req.nextUrl;
if (role === "ADMIN" && pathname.startsWith("/dashboard")) {
return NextResponse.redirect(new URL("/admin", req.url));
}
if (role !== "ADMIN" && pathname.startsWith("/admin")) {
return NextResponse.redirect(new URL("/dashboard", req.url));
}
// Restrict non-approved accounts from app surfaces
if (role !== "ADMIN") {
if (status === "APPROVED" && pathname === "/pending") {
return NextResponse.redirect(new URL("/dashboard", req.url));
}
const isRestrictedStatus = status === "PENDING" || status === "REJECTED" || status === "DISABLED";
if (isRestrictedStatus && pathname !== "/pending") {
return NextResponse.redirect(new URL("/pending", req.url));
}
}
return NextResponse.next();
},
{
callbacks: {
authorized: ({ token }) => !!token,
},
}
);
export const config = {
matcher: ["/dashboard/:path*", "/admin/:path*", "/mya-chat/:path*", "/pending"],
};