-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathmiddleware.js
38 lines (36 loc) · 1.1 KB
/
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
30
31
32
33
34
35
36
37
38
import { cookies } from "next/headers";
import { NextResponse } from "next/server";
export const middleware = (request) => {
const authToken = cookies().get("authToken")?.value || "";
let path = request.nextUrl.pathname;
if (
path === "/api/login" ||
path === "/api/signup" ||
path === "/api/login-user" ||
path === "/api/product" ||
/^\/api\/product\/\w+$/.test(path) ||
path === "/api/cart" ||
path === "/api/category" ||
/^\/api\/category\/\w+$/.test(path) ||
path === "/api/relatedProducts" ||
/^\/api\/relatedProducts\/\w+$/.test(path)
) {
return null;
}
const loggedInUserNotAccessPath =
path === "/loginpage" || path === "/signupPage";
if (loggedInUserNotAccessPath) {
if (authToken) {
return NextResponse.redirect(new URL("/", request.nextUrl));
}
} else {
if (!authToken) {
if (path.startsWith("/api") || path === "/dashboard") {
return NextResponse.redirect(new URL("/", request.nextUrl));
}
}
}
};
export const config = {
matcher: ["/", "/loginpage", "/signupPage", "/dashboard", "/api/:path*"],
};