From f3534dc54f2bd38dee640fa7e53e7c490c2e1773 Mon Sep 17 00:00:00 2001 From: aliyss Date: Thu, 17 Aug 2023 20:31:04 +0200 Subject: [PATCH] Cookies get updated if session returns Updated Cookies --- packages/qwik-auth/src/index.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/qwik-auth/src/index.ts b/packages/qwik-auth/src/index.ts index 4423157a5fe..1fcb02385ff 100644 --- a/packages/qwik-auth/src/index.ts +++ b/packages/qwik-auth/src/index.ts @@ -12,7 +12,7 @@ import { import { isServer } from '@builder.io/qwik/build'; import { parseString, splitCookiesString } from 'set-cookie-parser'; -export type GetSessionResult = Promise; +export type GetSessionResult = Promise<{ data: Session | null; cookie: any }>; export type QwikAuthConfig = AuthConfig; const actions: AuthAction[] = [ @@ -104,7 +104,12 @@ export function serverAuthQrl(authOptions: QRL<(ev: RequestEventCommon) => QwikA } throw req.send(res); } else { - req.sharedMap.set('session', await getSessionData(req.request, auth)); + const { data, cookie } = await getSessionData(req.request, auth); + req.sharedMap.set('session', data); + if (cookie) { + req.headers.set('set-cookie', cookie); + fixCookies(req); + } } } }; @@ -178,11 +183,13 @@ async function getSessionData(req: Request, options: AuthConfig): GetSessionResu const { status = 200 } = response; const data = await response.json(); + const cookie = response.headers.get('set-cookie'); if (!data || !Object.keys(data).length) { - return null; + return { data: null, cookie }; } if (status === 200) { - return data; + return { data, cookie }; } + throw new Error(data.message); }