-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmiddleware.js
29 lines (27 loc) · 850 Bytes
/
middleware.js
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
import { auth } from "@/utils/auth";
import { NextResponse } from "next/server";
export default auth((req) => {
const url = req.nextUrl;
const path = url.pathname;
const session = req.auth;
let publicPath =
path === "/" ||
path === "/signin" ||
path === "/signup" ||
path === "/forgot-password" ||
path === "/reset-password" ||
path === "/verify-email";
if (session && publicPath) {
return NextResponse.redirect(new URL("/dashboard", req.url));
}
if (!session && !publicPath) {
return NextResponse.redirect(new URL("/signin", req.url));
}
const adminPath = path.startsWith("/admin");
if (adminPath && session.role !== "ADMIN") {
return NextResponse.redirect(new URL("/dashboard", req.url));
}
});
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};