owltide/server/api/unsubscribe.post.ts
Hornwitser 52dfde95d1 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.
2025-03-07 15:47:48 +01:00

16 lines
581 B
TypeScript

import { readSubscriptions, writeSubscriptions } from "~/server/database";
export default defineEventHandler(async (event) => {
const session = await requireAccountSession(event);
const subscriptions = await readSubscriptions();
const existingIndex = subscriptions.findIndex(
sub => sub.type === "push" && sub.sessionId === session.id
);
if (existingIndex !== -1) {
subscriptions.splice(existingIndex, 1);
} else {
return { message: "No subscription registered."};
}
await writeSubscriptions(subscriptions);
return { message: "Existing subscription removed."};
});