Skip to content

Commit 8a88cd7

Browse files
committed
fix: preserve custom cookies when setting headers in ExpressServer
AdminForth/1900/image
1 parent fe5fc7b commit 8a88cd7

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎adminforth/servers/express.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,12 @@ class ExpressServer implements IExpressHttpServer {
763763
}
764764

765765
response.headers.forEach(([name, value]) => {
766-
res.setHeader(name, value);
766+
// setHeader replaces previous value, so cookies set by hooks would be lost behind auth cookie
767+
if (name.toLowerCase() === 'set-cookie') {
768+
res.append(name, value);
769+
} else {
770+
res.setHeader(name, value);
771+
}
767772
});
768773
res.status(response.status);
769774
if (response.message) {

‎tests/jest_tests/logout_hook.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ describe('auth.beforeLogout', () => {
7575
expect(res.headers['set-cookie'][0]).toContain('Expires=Thu, 01 Jan 1970 00:00:00 GMT');
7676
});
7777

78+
it('can remove custom cookies together with auth cookie', async () => {
79+
hooks.push(async ({ adminforth, extra }) => {
80+
adminforth.auth.removeCustomCookie({ response: extra.response, name: 'plugin_state' });
81+
});
82+
83+
const res = await logout(await loginCookie());
84+
85+
const brandSlug = admin.config.customization.brandNameSlug;
86+
expect(res.headers['set-cookie']).toEqual([
87+
expect.stringContaining(`adminforth_${brandSlug}_plugin_state=;`),
88+
expect.stringContaining(`adminforth_${brandSlug}_jwt=;`),
89+
]);
90+
});
91+
7892
it('clears auth cookie and runs remaining hooks when a hook throws', async () => {
7993
const called: string[] = [];
8094
hooks.push(

0 commit comments

Comments
 (0)