Skip to content

Commit e320385

Browse files
authored
fix host cookie deleting (#1011)
<!-- Make sure you've read the CONTRIBUTING.md guidelines: https://github.com/stack-auth/stack-auth/blob/dev/CONTRIBUTING.md --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Cookie deletion now respects the browser's secure context when domains are specified, ensuring consistent removal across secure and non-secure connections. * Session refresh logic now removes redundant default cookies after setting a custom refresh cookie, preventing duplicate cookies and conflicts. * **Tests** * End-to-end tests updated to expect the default refresh cookie to be absent and to read payloads from the custom cookie. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 8fa56f8 commit e320385

3 files changed

Lines changed: 6 additions & 7 deletions

File tree

apps/e2e/tests/js/cookies.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,10 @@ it("should set refresh token cookies for trusted parent domains", async ({ expec
166166
const customReady = await waitUntil(() => cookieStore.has(customCookieName), 10_000);
167167
expect(customReady).toBe(true);
168168

169-
expect(cookieStore.has(defaultCookieName)).toBe(true);
169+
expect(cookieStore.has(defaultCookieName)).toBe(false);
170170
expect(cookieStore.has(customCookieName)).toBe(true);
171171

172-
const valuesEqual = await waitUntil(() => cookieStore.get(customCookieName) === cookieStore.get(defaultCookieName), 10_000);
173-
expect(valuesEqual).toBe(true);
174-
175-
const defaultValue = cookieStore.get(defaultCookieName)!;
172+
const defaultValue = cookieStore.get(customCookieName)!;
176173
const parsedValue = JSON.parse(decodeURIComponent(defaultValue));
177174
expect(typeof parsedValue.refresh_token).toBe("string");
178175
expect(parsedValue.refresh_token.length).toBeGreaterThan(10);

packages/template/src/lib/cookie.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ function setCookieClientInternal(name: string, value: string, options: SetCookie
204204

205205
function deleteCookieClientInternal(name: string, options: DeleteCookieOptions = {}) {
206206
if (options.domain !== undefined) {
207-
Cookies.remove(name, { domain: options.domain });
207+
Cookies.remove(name, { domain: options.domain, secure: determineSecureFromClientContext() });
208208
}
209-
Cookies.remove(name);
209+
Cookies.remove(name, { secure: determineSecureFromClientContext() });
210210
}
211211

212212
export function setOrDeleteCookieClient(name: string, value: string | null, options: SetCookieOptions & DeleteCookieOptions = {}) {

packages/template/src/lib/stack-app/apps/implementations/client-app-impl.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,8 @@ export class _StackClientAppImplIncomplete<HasTokenStore extends boolean, Projec
620620
}
621621
const value = refreshToken && updatedAt ? this._formatRefreshCookieValue(refreshToken, updatedAt) : null;
622622
await setCookie(domain.data, value);
623+
const isSecure = await isSecureCookieContext();
624+
await setOrDeleteCookie(this._getRefreshTokenDefaultCookieNameForSecure(isSecure), null);
623625
});
624626
}
625627
private async _getTrustedParentDomain(currentDomain: string): Promise<string | null> {

0 commit comments

Comments
 (0)