From 3a6db88da2fb4e78bc6c8441eed630dd24e12d95 Mon Sep 17 00:00:00 2001 From: Brandon Bayer Date: Tue, 18 Jan 2022 11:20:32 -0500 Subject: [PATCH] fix passport adapter to work behind cloudfront --- nextjs/packages/next/stdlib-server/middleware.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/nextjs/packages/next/stdlib-server/middleware.ts b/nextjs/packages/next/stdlib-server/middleware.ts index 4a27aea166..5f9047bb36 100644 --- a/nextjs/packages/next/stdlib-server/middleware.ts +++ b/nextjs/packages/next/stdlib-server/middleware.ts @@ -149,10 +149,14 @@ function getProtocol(req: MiddlewareRequest) { if (req.connection.encrypted) { return 'https' } + + if (!req.headers) return 'http' + const forwardedProto = - req.headers && - ((req.headers['forwarded'] as string)?.match(/(?<=proto=).+/g)?.[0] || - (req.headers['x-forwarded-proto'] as string)) + (req.headers['forwarded'] as string)?.match(/(?<=proto=).+/g)?.[0] || + (req.headers['x-forwarded-proto'] as string) || + (req.headers['CloudFront-Forwarded-Proto'] as string) + if (forwardedProto) { return forwardedProto.split(/\s*,\s*/)[0] }