Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Stored error-log audio (admin playback)
service/error_audio/

# macOS
.DS_Store

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
4 changes: 4 additions & 0 deletions app/.env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
DATABASE_URL=XXXXXXXXXXX
NEXT_PUBLIC_MICROSERVICE_URL=XXXXXXXXXXX

# Basic auth for /admin routes (format: user:password or user:password|user2:password2)
# Optional: when set, visiting /admin/* will prompt for these credentials.
BASIC_AUTH_CREDENTIALS=admin:your-secure-password
AWS_REGION=XXXXXXXXXXX
AWS_ACCESS_KEY_ID=XXXXXXXXXXX
AWS_SECRET_ACCESS_KEY=XXXXXXXXXXX
Expand Down
38 changes: 38 additions & 0 deletions app/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { createNextAuthMiddleware } from "nextjs-basic-auth-middleware";
import { NextRequest, NextResponse } from "next/server";

/**
* Parse BASIC_AUTH_CREDENTIALS env (format: user:password or user:password|user2:password2)
* into users array. Package expects { name, password } per user.
*/
function getUsersFromEnv(): { name: string; password: string }[] {
const raw = process.env.BASIC_AUTH_CREDENTIALS;
if (!raw?.trim()) return [];
return raw
.split("|")
.map((pair) => {
const [name, ...rest] = pair.trim().split(":");
const password = rest.join(":").trim();
return name && password ? { name, password } : null;
})
.filter((u): u is { name: string; password: string } => u !== null);
}

export const config = {
// Protect only /admin routes. Use ['/(.*)'] to protect the whole app.
matcher: ["/admin/:path*"],
};

export default function middleware(req: NextRequest) {
const users = getUsersFromEnv();
// Only run basic auth when credentials are configured
if (users.length === 0) {
return NextResponse.next();
}
const authMiddleware = createNextAuthMiddleware({
users,
realm: "Admin",
message: "Authentication failed",
});
return authMiddleware(req);
}
Loading