Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(auth): Cookies get updated if session returns Updated Cookies #4960

Merged
merged 1 commit into from
Aug 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}