From b6e6f1ec58d572a5137dd4580b139ac62f6f201c Mon Sep 17 00:00:00 2001 From: Tobbe Lundberg Date: Tue, 12 Nov 2024 14:56:22 +0100 Subject: [PATCH] fix(dbAuth): Don't use Multi Value Headers on Vercel (#11718) --- .changesets/11718.md | 3 +++ .../auth-providers/dbAuth/api/src/shared.ts | 20 +++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 .changesets/11718.md diff --git a/.changesets/11718.md b/.changesets/11718.md new file mode 100644 index 000000000000..09c40430f2b0 --- /dev/null +++ b/.changesets/11718.md @@ -0,0 +1,3 @@ +- fix(dbAuth): Don't use Multi Value Headers on Vercel (#11718) by @Tobbe + +Fixes a regression regarding dbAuth on Vercel introduced in RW 8 diff --git a/packages/auth-providers/dbAuth/api/src/shared.ts b/packages/auth-providers/dbAuth/api/src/shared.ts index 342ad8ba9310..ba7417150767 100644 --- a/packages/auth-providers/dbAuth/api/src/shared.ts +++ b/packages/auth-providers/dbAuth/api/src/shared.ts @@ -295,12 +295,18 @@ export function getDbAuthResponseBuilder( const setCookieHeaders = response.headers?.getSetCookie() || [] if (setCookieHeaders.length > 0) { - if ('multiValueHeaders' in event) { + delete headers['set-cookie'] + delete headers['Set-Cookie'] + + if (supportsMultiValueHeaders(event)) { dbAuthResponse.multiValueHeaders = { + // Netlify wants 'Set-Cookie' headers to be capitalized + // https://github.com/redwoodjs/redwood/pull/10889 'Set-Cookie': setCookieHeaders, } - delete headers['set-cookie'] } else { + // If we do this for Netlify the lambda function will throw an error + // https://github.com/redwoodjs/redwood/pull/10889 headers['set-cookie'] = setCookieHeaders } } @@ -309,6 +315,16 @@ export function getDbAuthResponseBuilder( } } +// `'multiValueHeaders' in event` is true for both Netlify and Vercel +// but only Netlify actually supports it. Vercel will just ignore it +// https://github.com/vercel/vercel/issues/7820 +function supportsMultiValueHeaders(event: APIGatewayProxyEvent | Request) { + return ( + 'multiValueHeaders' in event && + (!event.headers || !('x-vercel-id' in event.headers)) + ) +} + export const extractHashingOptions = (text: string): ScryptOptions => { const [_hash, ...options] = text.split('|')