-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
27 lines (22 loc) · 796 Bytes
/
Copy pathmiddleware.ts
File metadata and controls
27 lines (22 loc) · 796 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
import { NextRequest, NextResponse } from "next/server";
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Only protect /admin routes (except the login page and login API)
if (
pathname.startsWith("/admin") &&
!pathname.startsWith("/admin/login") &&
!pathname.startsWith("/api/admin/login")
) {
const token = request.cookies.get("axon-admin-token")?.value;
const expected = process.env.ADMIN_SESSION_TOKEN;
if (!token || !expected || token !== expected) {
const loginUrl = new URL("/admin/login", request.url);
loginUrl.searchParams.set("redirect", pathname);
return NextResponse.redirect(loginUrl);
}
}
return NextResponse.next();
}
export const config = {
matcher: ["/admin/:path*"],
};