-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
41 lines (35 loc) · 1.16 KB
/
middleware.ts
File metadata and controls
41 lines (35 loc) · 1.16 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
36
37
38
39
40
41
// middleware.ts (create this in your project root)
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(request: NextRequest) {
// Only run middleware on the studio path
if (request.nextUrl.pathname.startsWith("/admin")) {
const authHeader = request.headers.get("authorization");
if (!authHeader || !isValidAuth(authHeader)) {
return new NextResponse(null, {
status: 401,
headers: {
"WWW-Authenticate": 'Basic realm="Sanity Studio Access"',
},
});
}
}
return NextResponse.next();
}
function isValidAuth(authHeader: string): boolean {
// Basic authentication format: "Basic base64(username:password)"
const base64Credentials = authHeader.split(" ")[1];
const credentials = Buffer.from(base64Credentials, "base64").toString(
"ascii"
);
const [username, password] = credentials.split(":");
// Compare with environment variables
return (
username === process.env.ADMIN_USERNAME &&
password === process.env.ADMIN_PASSWORD
);
}
// Configure which paths the middleware runs on
export const config = {
matcher: "/admin/:path*",
};