Adding login admin, fix ipv6 Vienamese version, add env example

This commit is contained in:
2026-05-15 19:52:34 +07:00
parent 31aebc40f0
commit c7f09d941e
15 changed files with 334 additions and 66 deletions
+6 -33
View File
@@ -1,41 +1,14 @@
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
function unauthorized() {
return new NextResponse("Authentication required", {
status: 401,
headers: {
"WWW-Authenticate": 'Basic realm="IPv6 Admin"',
},
});
}
export function middleware(request: NextRequest) {
const user = process.env.ADMIN_BASIC_AUTH_USER;
const pass = process.env.ADMIN_BASIC_AUTH_PASS;
const session = request.cookies.get("admin_session");
if (!user || !pass) {
return NextResponse.next();
}
const auth = request.headers.get("authorization");
if (!auth?.startsWith("Basic ")) {
return unauthorized();
}
let decoded: string;
try {
decoded = atob(auth.slice(6));
} catch {
return unauthorized();
}
const colon = decoded.indexOf(":");
const u = colon >= 0 ? decoded.slice(0, colon) : "";
const p = colon >= 0 ? decoded.slice(colon + 1) : "";
if (u !== user || p !== pass) {
return new NextResponse("Unauthorized", { status: 401 });
if (!session || session.value !== "true") {
// If not authenticated and trying to access admin, redirect to login
const url = request.nextUrl.clone();
url.pathname = "/login";
return NextResponse.redirect(url);
}
return NextResponse.next();