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:
parent
150cb82f5c
commit
52dfde95d1
7 changed files with 39 additions and 15 deletions
|
@ -1,7 +1,16 @@
|
|||
import type { H3Event } from "h3";
|
||||
import { nextSessionId, readSessions, writeSessions } from "~/server/database";
|
||||
import { nextSessionId, readSessions, readSubscriptions, writeSessions, writeSubscriptions } from "~/server/database";
|
||||
import { Session } from "~/shared/types/account";
|
||||
|
||||
async function removeSessionSubscription(sessionId: number) {
|
||||
const subscriptions = await readSubscriptions();
|
||||
const index = subscriptions.findIndex(subscription => subscription.sessionId === sessionId);
|
||||
if (index !== -1) {
|
||||
subscriptions.splice(index, 1);
|
||||
await writeSubscriptions(subscriptions);
|
||||
}
|
||||
}
|
||||
|
||||
async function clearAccountSessionInternal(event: H3Event, sessions: Session[]) {
|
||||
const existingSessionCookie = await getSignedCookie(event, "session");
|
||||
if (existingSessionCookie) {
|
||||
|
@ -9,6 +18,7 @@ async function clearAccountSessionInternal(event: H3Event, sessions: Session[])
|
|||
const sessionIndex = sessions.findIndex(session => session.id === sessionId);
|
||||
if (sessionIndex !== -1) {
|
||||
sessions.splice(sessionIndex, 1);
|
||||
await removeSessionSubscription(sessionId);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue