Skip to content

Commit

Permalink
Cookies get updated if session returns Updated Cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
aliyss committed Aug 17, 2023
1 parent 47c2d1e commit f3534dc
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/qwik-auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { isServer } from '@builder.io/qwik/build';
import { parseString, splitCookiesString } from 'set-cookie-parser';

export type GetSessionResult = Promise<Session | null>;
export type GetSessionResult = Promise<{ data: Session | null; cookie: any }>;
export type QwikAuthConfig = AuthConfig;

const actions: AuthAction[] = [
Expand Down Expand Up @@ -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);
}
}
}
};
Expand Down Expand Up @@ -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);
}

0 comments on commit f3534dc

Please sign in to comment.