diff --git a/src/frontend/middleware.ts b/src/frontend/middleware.ts index c28e0b2f..e02c620e 100644 --- a/src/frontend/middleware.ts +++ b/src/frontend/middleware.ts @@ -1,33 +1,22 @@ -import { NextRequest, NextResponse } from "next/server"; +import { NextRequest, NextResponse } from 'next/server' export const config = { - matcher: ["/", "/index"], -}; - -export function middleware(req: NextRequest) { - // Getting the Pup IP from the request - const { ip } = req; - // console.log("Middleware IP:", ip); - const basicAuth = req.headers.get("authorization"); - const url = req.nextUrl; + matcher: ['/', '/index'], +} - // Bypass the basic auth on a certain env variable and Pub IP - if ( - process.env.LOCAL_URL === "http://localhost:3000" - ) { - if (basicAuth) { - const authValue = basicAuth.split(" ")[1]; - const [user, pwd] = atob(authValue).split(":"); +export default function middleware(req: NextRequest) { + const basicAuth = req.headers.get('authorization') + const url = req.nextUrl - const validUser = process.env.BASIC_AUTH_USER; - const validPassWord = process.env.BASIC_AUTH_PASSWORD; + if (basicAuth) { + const authValue = basicAuth.split(' ')[1] + const [user, pwd] = atob(authValue).split(':') - if (user === validUser && pwd === validPassWord) { - return NextResponse.next(); - } + if (user === 'admin' && pwd === 'civicactions') { + return NextResponse.next() } - url.pathname = "/api/basicauth"; - - return NextResponse.rewrite(url); } + url.pathname = '/api/basic-auth' + + return NextResponse.rewrite(url) } diff --git a/src/frontend/pages/api/basic-auth.ts b/src/frontend/pages/api/basic-auth.ts index 47bd05a9..154ba5fb 100644 --- a/src/frontend/pages/api/basic-auth.ts +++ b/src/frontend/pages/api/basic-auth.ts @@ -1,9 +1,7 @@ -export async function GET(request: Request) { - console.log("GET /api/basicauth/route.ts"); - return new Response("Authentication Required!", { - status: 401, - headers: { - "WWW-Authenticate": "Basic realm='private_pages'", - }, - }); -} \ No newline at end of file +import type { NextApiRequest, NextApiResponse } from 'next' + +export default function handler(_: NextApiRequest, res: NextApiResponse) { + res.setHeader('WWW-authenticate', 'Basic realm="Secure Area"') + res.statusCode = 401 + res.end(`Auth Required.`) +}