-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
35 lines (26 loc) · 1.02 KB
/
Copy pathmiddleware.ts
File metadata and controls
35 lines (26 loc) · 1.02 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(request: NextRequest) {
const session = request.cookies.get("session")?.value;
const { pathname } = request.nextUrl;
const isLoginPage = pathname === "/admin/login";
const isHostLoginPage = pathname.startsWith("/host/login");
const isAdminRoute = pathname.startsWith("/admin");
const isHostRoute = pathname.startsWith("/us/host");
if (isAdminRoute && !isLoginPage && !session) {
return NextResponse.redirect(new URL("/admin/login", request.url));
}
if (isLoginPage && session) {
return NextResponse.redirect(new URL("/admin", request.url));
}
if (isHostRoute && !isHostLoginPage && !session) {
return NextResponse.redirect(new URL("/host/login", request.url));
}
if (isHostLoginPage && session) {
return NextResponse.redirect(new URL("/host", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/admin/:path*", "/us/host/:path*"],
};