-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from acabouet/pgov-440-basic-auth-frontend
PGOV-440: Basic auth for frontend application and fix for broken images
- Loading branch information
Showing
5 changed files
with
67 additions
and
2 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
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { NextRequest, NextResponse } from 'next/server' | ||
|
||
export const config = { | ||
matcher: ['/', '/index'], | ||
} | ||
|
||
export default function middleware(req: NextRequest) { | ||
const basicAuth = req.headers.get('authorization') | ||
const url = req.nextUrl | ||
|
||
if (basicAuth) { | ||
const authValue = basicAuth.split(' ')[1] | ||
const [user, pwd] = atob(authValue).split(':') | ||
|
||
if (user === process.env.BASIC_AUTH_USER && pwd === process.env.BASIC_AUTH_PASSWORD) { | ||
return NextResponse.next() | ||
} | ||
} | ||
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
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
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
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.`) | ||
} |