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.
19 lines
605 B
TypeScript
19 lines
605 B
TypeScript
import { readAccounts, readSubscriptions } from "~/server/database";
|
|
import { AccountSession } from "~/shared/types/account";
|
|
|
|
export default defineEventHandler(async (event): Promise<AccountSession | undefined> => {
|
|
const session = await getAccountSession(event);
|
|
if (!session)
|
|
return;
|
|
const accounts = await readAccounts();
|
|
const subscriptions = await readSubscriptions();
|
|
const push = Boolean(
|
|
subscriptions.find(sub => sub.type === "push" && sub.sessionId === session.id)
|
|
);
|
|
|
|
return {
|
|
id: session.id,
|
|
account: accounts.find(account => account.id === session.accountId)!,
|
|
push,
|
|
};
|
|
})
|