-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
34 lines (26 loc) · 838 Bytes
/
middleware.ts
File metadata and controls
34 lines (26 loc) · 838 Bytes
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
import { NextRequest, NextResponse } from "next/server";
import { apiFetchServer } from "./lib/apiFetchServer";
const LOGIN = "/auth/login";
const HOME = "/";
const checkIsLogged = async () => {
try {
const res = await apiFetchServer("/api/Users/me");
return res.ok;
} catch (error) {
return false;
}
};
export async function middleware(request: NextRequest) {
const isLoggedIn = await checkIsLogged();
const requestPath = request.nextUrl.pathname;
if (!isLoggedIn) {
return NextResponse.redirect(new URL(LOGIN, request.url));
}
if (isLoggedIn && (requestPath === "/" || requestPath.includes("/auth"))) {
return NextResponse.redirect(new URL(HOME, request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/home", "/convert"],
};