From 63bf354c74a4a6f57734d25bd42d0ddd8710029d Mon Sep 17 00:00:00 2001 From: Eduardo Stefani Pacheco Date: Wed, 27 Mar 2024 07:48:35 +0800 Subject: [PATCH] Fix configuring basic auth docs (#3716) * Fix configuring basic auth docs * Update www/docs/constructs/NextjsSite.about.md Apply fix from @Roman86 Co-authored-by: Roman Kozodoi --------- Co-authored-by: Eduardo Pacheco Co-authored-by: Roman Kozodoi --- www/docs/constructs/NextjsSite.about.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/www/docs/constructs/NextjsSite.about.md b/www/docs/constructs/NextjsSite.about.md index 843bc2821d..c58df151ef 100644 --- a/www/docs/constructs/NextjsSite.about.md +++ b/www/docs/constructs/NextjsSite.about.md @@ -494,15 +494,16 @@ new NextjsSite(stack, "Site", { transform: (plan) => { const username = "admin"; const password = "P@ssw0rd!"; + const basicAuth = Buffer.from(`${username}:${password}`).toString("base64"); plan.cloudFrontFunctions.serverCfFunction.injections.push(` - var authHeaders = request.headers.authorization; - if (!authHeaders || authHeaders.value !== "Basic ${Buffer.from(${username}:${password}).toString('base64') - }"}) { - statusCode: 401, - statusDescription: "Unauthorized", - headers: { - "www-authenticate": { value: 'Basic realm="Secure Area"' } - } + if (request?.headers?.authorization?.value !== 'Basic ${basicAuth}') { + return { + statusCode: 401, + statusDescription: "Unauthorized", + headers: { + "www-authenticate": { value: 'Basic realm="Secure Area"' }, + }, + }; } `); }, @@ -575,7 +576,7 @@ new NextjsSite(stack, "Site", { "OriginAccessIdentity", "XXXXXXXX" ), - }, + }, }, }); ```