forked from M74JJI/shoppay-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
24 lines (23 loc) · 805 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
import { getToken } from "next-auth/jwt";
import { NextResponse } from "next/server";
export async function middleware(req) {
const { pathname, origin } = req.nextUrl;
const session = await getToken({
req,
secret: process.env.JWT_SECRET,
secureCookie: process.env.NODE_ENV === "production",
});
if (pathname == "/checkout") {
if (!session) return NextResponse.redirect(`${origin}`);
}
if (pathname.startsWith("/order")) {
if (!session) return NextResponse.redirect(`${origin}`);
}
if (pathname.startsWith("/profile")) {
if (!session) return NextResponse.redirect(`${origin}`);
}
if (pathname.startsWith("/admin")) {
if (!session) return NextResponse.redirect(`${origin}`);
if (session.role !== "admin") return NextResponse.redirect(`${origin}`);
}
}