-
Notifications
You must be signed in to change notification settings - Fork 16
/
middleware.ts
41 lines (33 loc) · 1.35 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
import { NextResponse } from "next/server";
import { NextRequest } from "next/server";
const protectedRoutes = ["/academia", "/courses"];
const home = ["/"];
const MAINTENANCE = false;
const isAuthenticated = (request: NextRequest): boolean => {
const token = request.cookies.get("key");
return !!token;
};
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
const token = request.cookies.get("key");
if (MAINTENANCE && pathname !== "/maintenance") {
return NextResponse.redirect(new URL("/maintenance", request.url));
} else if (!MAINTENANCE && pathname === "/maintenance") {
return NextResponse.redirect(new URL("/", request.url));
}
if (isAuthenticated(request) && home.includes(pathname)) {
if (token && token.value && token.value.length <= 500) {
return NextResponse.redirect(new URL("/auth/logout", request.url));
}
return NextResponse.redirect(new URL("/academia", request.url));
} else if (!isAuthenticated(request) && home.includes(pathname)) {
return NextResponse.redirect(new URL("/home", request.url));
}
if (protectedRoutes.includes(pathname) && !isAuthenticated(request)) {
return NextResponse.redirect(new URL("/auth/login", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};