-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
53 lines (46 loc) · 1.33 KB
/
middleware.ts
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
43
44
45
46
47
48
49
50
51
52
53
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(request: NextRequest) {
const path = request.nextUrl.pathname;
const publicPaths = [
"/",
"/login",
"/sign-up",
"/forget-password",
"/reset-password",
"/verify-otp",
];
const privatePaths = ["/feed", /^\/profile\/[a-zA-Z0-9]+$/];
const isPrivatePath = (path:string | RegExp) => {
return privatePaths.some((privatePath) => {
if (typeof privatePath === "string") {
return privatePath === path; //
} else if (privatePath instanceof RegExp) {
return privatePath.test(path as string);
}
return false;
});
};
console.log("COOKIES REQUEST",request.cookies)
const token = request.cookies.get("accessToken")?.value;
if (publicPaths.includes(path) && token) {
return NextResponse.redirect(new URL("/feed", request.nextUrl));
}
if (isPrivatePath(path) && !token) {
console.log("isPrivatePath Valid")
return NextResponse.redirect(new URL("/login", request.nextUrl));
}
return NextResponse.next();
}
export const config = {
matcher: [
"/",
"/login",
"/sign-up",
"/forget-password",
"/reset-password",
"/verify-otp",
"/feed",
"/profile"
],
};