-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmiddleware.ts
More file actions
98 lines (79 loc) · 2.47 KB
/
Copy pathmiddleware.ts
File metadata and controls
98 lines (79 loc) · 2.47 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { cookies } from "next/headers";
import { getUser } from "./app/actions/user";
function matchURL(url: string, matchURL: string) {
const regex = new RegExp(matchURL);
return regex.test(url);
}
export async function middleware(req: NextRequest) {
const res = NextResponse.next();
const token = await getUser();
if (token.success === false) {
if (matchURL(req.nextUrl.pathname, "/home(.*)")) {
const redirectUrl = req.nextUrl.clone();
redirectUrl.pathname = "/";
return NextResponse.redirect(redirectUrl);
}
if (matchURL(req.nextUrl.pathname, "/my-projects(.*)")) {
const redirectUrl = req.nextUrl.clone();
redirectUrl.pathname = "/";
return NextResponse.redirect(redirectUrl);
}
if (matchURL(req.nextUrl.pathname, "/projects(.*)")) {
return NextResponse.next();
}
if (matchURL(req.nextUrl.pathname, "/courses(.*)")) {
return NextResponse.next();
}
if (matchURL(req.nextUrl.pathname, "/auth/login(.*)")) {
return NextResponse.next();
}
if (matchURL(req.nextUrl.pathname, "/auth/register(.*)")) {
return NextResponse.next();
}
if (matchURL(req.nextUrl.pathname, "/(.*)")) {
return NextResponse.next();
}
}
if (token.success === true) {
if (matchURL(req.nextUrl.pathname, "/home(.*)")) {
const redirectUrl = req.nextUrl.clone();
redirectUrl.pathname = "/";
return NextResponse.redirect(redirectUrl);
}
if (matchURL(req.nextUrl.pathname, "/projects(.*)")) {
return NextResponse.next();
}
if (matchURL(req.nextUrl.pathname, "/my-projects(.*)")) {
return NextResponse.next();
}
if (matchURL(req.nextUrl.pathname, "/courses(.*)")) {
return NextResponse.next();
}
if (matchURL(req.nextUrl.pathname, "/auth/login(.*)")) {
const redirectUrl = req.nextUrl.clone();
redirectUrl.pathname = "/";
return NextResponse.redirect(redirectUrl);
}
if (matchURL(req.nextUrl.pathname, "/auth/register(.*)")) {
const redirectUrl = req.nextUrl.clone();
redirectUrl.pathname = "/";
return NextResponse.redirect(redirectUrl);
}
if (matchURL(req.nextUrl.pathname, "/(.*)")) {
return NextResponse.next();
}
}
return res;
}
export const config = {
matcher: [
"/",
"/auth/login",
"/auth/register",
"/courses",
"/projects",
"/my-projects",
],
};