Tie push subscriptions to current session

If a user logs out from a device the expectation should be that device
no longer having any association with the user's account.  Any existing
push notifications should thefore be removed on server.  For this reason
tie push notifications to a session, and remove them when the session is
deleted.
This commit is contained in:
Hornwitser 2025-03-07 14:11:07 +01:00
parent 150cb82f5c
commit 52dfde95d1
7 changed files with 39 additions and 15 deletions

View file

@ -2,12 +2,17 @@ import { readSubscriptions, writeSubscriptions } from "~/server/database";
import { Subscription } from "~/shared/types/account";
export default defineEventHandler(async (event) => {
const session = await requireAccountSession(event);
const body: { subscription: PushSubscriptionJSON } = await readBody(event);
const subscriptions = await readSubscriptions();
const existingIndex = subscriptions.findIndex(
sub => sub.type === "push" && sub.push.endpoint === body.subscription.endpoint
sub => sub.type === "push" && sub.sessionId === session.id
);
const subscription: Subscription = { type: "push", push: body.subscription };
const subscription: Subscription = {
type: "push",
sessionId: session.id,
push: body.subscription
};
if (existingIndex !== -1) {
subscriptions[existingIndex] = subscription;
} else {