-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
31 lines (26 loc) · 812 Bytes
/
middleware.ts
File metadata and controls
31 lines (26 loc) · 812 Bytes
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
import { NextResponse } from "next/server";
import { auth } from "./auth";
export default auth((req) => {
const { pathname } = req.nextUrl;
const session = !!req.auth;
const role = req.auth?.user.role || "guest";
const protectedPaths = [
/\/user\/bookings\/(.*)/,
/\/user\/comments/,
/\/user\/edit/,
/\/user\/like/,
/\/user\/mypage/,
/\/user\/rooms/,
/\/payments\/(.*)/,
/\/rooms\/register\/(.*)/,
/\/rooms\/edit\/(.*)/,
];
const adminPaths = [/\/admin\/(.*)/];
if (!session && protectedPaths.some((p) => p.test(pathname))) {
return NextResponse.redirect(new URL("/login", req.url));
}
if (role !== "ADMIN" && adminPaths.some((p) => p.test(pathname))) {
return NextResponse.redirect(new URL("/", req.url));
}
return NextResponse.next();
});