Skip to content

Commit

Permalink
pgov-440: update path for auth endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrienne committed Jan 27, 2025
1 parent 214ab00 commit 57ca0fa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 34 deletions.
39 changes: 14 additions & 25 deletions src/frontend/middleware.ts
Original file line number Diff line number Diff line change
@@ -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)
}
16 changes: 7 additions & 9 deletions src/frontend/pages/api/basic-auth.ts
Original file line number Diff line number Diff line change
@@ -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'",
},
});
}
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.`)
}

0 comments on commit 57ca0fa

Please sign in to comment.