mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 08:12:52 +00:00
fix(auth): take the push subscription off on every sign-out button
Only the menu's sign-out removed this browser's push subscription. The sign-out buttons on the registration page and the profile take-over page, which exist so someone else can use the browser next, left it behind, and the next person kept seeing the last account's reminders. All three now go through one signOutRequest. The browser's own subscription is now removed only once the server has confirmed the sign-out; the server row still goes first, while the session can name it. A refused sign-out stores the row again, so the person who is still signed in keeps push. The logout test pins the new order and the refusal. The helper file is renamed web-push-subscription.ts, since it now also saves a subscription for the settings card.
This commit is contained in:
1 parent
6914f844be
commit
367ba65bf6
10 files changed
+231
-69
No files matched your search
@@ -6,7 +6,7 @@
|
||||
* existed belongs to it. The app replaces the page in the history, so Back
|
||||
* does not show the form again.
|
||||
*/
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { page } from "vitest/browser";
|
||||
|
||||
import { queryKeys } from "@/lib/query-keys";
|
||||
@@ -48,6 +48,45 @@ describe("registration page", () => {
|
||||
});
|
||||
|
||||
describe("registration page, already signed in", () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("takes this browser's push subscription with it on sign-out", async () => {
|
||||
// The button exists so someone else can register on this browser; a
|
||||
// subscription left behind would show them the last account's reminders.
|
||||
const unsubscribe = vi.fn(async () => true);
|
||||
vi.spyOn(navigator.serviceWorker, "getRegistration").mockResolvedValue({
|
||||
pushManager: {
|
||||
getSubscription: async () => ({
|
||||
endpoint: "https://push.example.test/send/abc",
|
||||
unsubscribe,
|
||||
}),
|
||||
},
|
||||
} as unknown as ServiceWorkerRegistration);
|
||||
const api = stubFetch()
|
||||
.on("DELETE", "/api/notifications/web-push", { data: {} })
|
||||
.on("POST", "/api/auth/logout", { data: { loggedOut: true } });
|
||||
window.history.replaceState(null, "", "/auth/register?invite=hlv_x");
|
||||
const { screen } = await renderWithApp(<RegisterPage />, {
|
||||
user: { username: "signed-in" },
|
||||
});
|
||||
|
||||
await screen.getByTestId("register-sign-out").click();
|
||||
|
||||
await expect.poll(() => unsubscribe.mock.calls.length).toBe(1);
|
||||
const order = api.requests
|
||||
.map((r) => `${r.method} ${r.path.split("?")[0]}`)
|
||||
.filter((call) => !call.startsWith("GET "));
|
||||
expect(order).toEqual([
|
||||
"DELETE /api/notifications/web-push",
|
||||
"POST /api/auth/logout",
|
||||
]);
|
||||
expect(api.calls("DELETE", "/api/notifications/web-push")[0].body).toEqual({
|
||||
endpoint: "https://push.example.test/send/abc",
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps the page and says so when the server refuses the sign-out", async () => {
|
||||
const api = stubFetch().on("POST", "/api/auth/logout", {
|
||||
status: 403,
|
||||
|
||||
@@ -12,9 +12,13 @@ import { Label } from "@/components/ui/label";
|
||||
import { PasswordStrength } from "@/components/ui/password-strength";
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
import { detectBrowserTimezone } from "@/lib/tz/format";
|
||||
import { ApiError, apiFetch, apiPost } from "@/lib/api/api-fetch";
|
||||
import { ApiError, apiPost } from "@/lib/api/api-fetch";
|
||||
import { localizedApiError } from "@/lib/api/localized-error";
|
||||
import { clearCachesForSessionEnd, useAuth } from "@/hooks/use-auth";
|
||||
import {
|
||||
clearCachesForSessionEnd,
|
||||
signOutRequest,
|
||||
useAuth,
|
||||
} from "@/hooks/use-auth";
|
||||
|
||||
export default function RegisterPage() {
|
||||
const router = useRouter();
|
||||
@@ -89,7 +93,7 @@ export default function RegisterPage() {
|
||||
setError(null);
|
||||
setSigningOut(true);
|
||||
try {
|
||||
await apiFetch("/api/auth/logout", { method: "POST" });
|
||||
await signOutRequest();
|
||||
// Same session-end wipe the shell and the menu do: the QueryClient
|
||||
// outlives the navigation on this SPA and the health-data families are
|
||||
// not user-scoped, so nothing may survive into the next account.
|
||||
|
||||
Reference in new issue
Block a user