-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
59 lines (53 loc) · 2.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { NextResponse } from "next/server";
export async function middleware(request) {
function getCookie() {
var cookieArr = request.cookies.getAll();
for (var i = 0; i < cookieArr.length; i++) {
if (cookieArr[i].name === "isLogged") {
return cookieArr[i].value;
}
}
return null;
}
const getCurUser = async () => {
var isLogged = getCookie("isLogged");
try {
if (isLogged === "" || isLogged == null) {
if (request.nextUrl.pathname.startsWith("/")) {
const url = request.nextUrl.clone();
url.pathname = "/login";
return NextResponse.redirect(url);
}
if (request.nextUrl.pathname.startsWith("/blog")) {
const url = request.nextUrl.clone();
url.pathname = "/login";
return NextResponse.redirect(url);
}
if (request.nextUrl.pathname.startsWith("/profile")) {
const url = request.nextUrl.clone();
url.pathname = "/login";
return NextResponse.redirect(url);
}
if (request.nextUrl.pathname.startsWith("/event")) {
const url = request.nextUrl.clone();
url.pathname = "/login";
return NextResponse.redirect(url);
}
}
else {
if (request.nextUrl.pathname.startsWith("/login")) {
const url = request.nextUrl.clone();
url.pathname = "/";
return NextResponse.redirect(url);
}
}
} catch (error) {
console.log(error);
}
return NextResponse.next(); // Return NextResponse.next() by default
}
return await getCurUser(); // Return the result of getCurUser()
}
export const config = {
matcher: ['/blog/:path*', '/event/:path*', '/profile', '/'],
}