-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pgov-440: update path for auth endpoint.
- Loading branch information
Adrienne
committed
Jan 27, 2025
1 parent
214ab00
commit 57ca0fa
Showing
2 changed files
with
21 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.`) | ||
} |