-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
52 lines (44 loc) · 1.44 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export default function Middleware(request: NextRequest) {
const { host, pathname } = request.nextUrl;
const isPublicPath = pathname === "/login" || pathname === "/signup";
const isLoggedIn =
request.cookies.get("next-auth.session-token")?.value ||
request.cookies.get("token")?.value ||
"";
const loginAuthorPath = pathname === "/admin/login" || pathname === "/admin";
const authorPath =
pathname === "/courses/addCourse" || pathname === "/courses/editCourse";
const isAdmin = request.cookies.get("admin-token")?.value || "";
const isBuyPath = pathname === "/buy/(.*)";
if (isBuyPath && !isLoggedIn) {
return NextResponse.redirect(new URL("/login", request.nextUrl));
}
if (!isAdmin && authorPath) {
return NextResponse.redirect(new URL("/admin/login", request.nextUrl));
}
if (isAdmin && loginAuthorPath) {
return NextResponse.redirect(new URL("/dashboard", request.nextUrl));
}
if (isLoggedIn && isPublicPath) {
return NextResponse.redirect(new URL("/me", request.nextUrl));
}
if (!isLoggedIn && !isPublicPath) {
return NextResponse.redirect(new URL("/login", request.nextUrl));
}
}
export const config = {
matcher: [
"/me",
"/me/(.*)",
"/login",
"/admin",
"/admin/(.*)",
"/signup",
"/logout",
"/buy/(.*)",
"/courses/addCourse",
"/courses/editCourse",
],
};