-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
30 lines (28 loc) · 930 Bytes
/
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
import { NextResponse } from "next/server";
export default function middleware(request) {
const token = request.cookies.has("biofidelicXQuizAuth");
if (token) {
const url = request.nextUrl.clone();
if (url.pathname === "/log-in") {
url.pathname = "/";
return NextResponse.redirect(url);
} else if (url.pathname === "/sign-up") {
url.pathname = "/";
return NextResponse.redirect(url);
} else {
return NextResponse.rewrite(request.nextUrl);
}
} else {
const url = request.nextUrl.clone();
const regex = new RegExp(/\/[\S]{1,}\/[\S]{1,}\/[\S]{1,}\/[\S]{1,}/g);
const quizPagePath = regex.test(url.pathname);
if (quizPagePath) {
url.pathname = "/sign-up";
return NextResponse.redirect(url);
}
return NextResponse.rewrite(request.nextUrl);
}
}
export const config = {
matcher: ["/category/:path*/quiz/:path*", "/sign-up", "/log-in"],
};