fix(webpush): ensure the old endpoint is cleared only when necessary

Signed-off-by: 0xsysr3ll <0xsysr3ll@pm.me>
This commit is contained in:
0xsysr3ll
2025-12-08 23:51:41 +01:00
parent 85bca35f98
commit 39e6115467

View File

@@ -73,18 +73,21 @@ export const verifyAndResubscribePushSubscription = async (
return false; return false;
} }
const { subscription } = await getPushSubscription();
const isValid = await verifyPushSubscription(userId, currentSettings); const isValid = await verifyPushSubscription(userId, currentSettings);
if (isValid) { if (isValid) {
return true; return true;
} }
if (subscription) {
return false;
}
if (currentSettings.enablePushRegistration) { if (currentSettings.enablePushRegistration) {
try { try {
// Unsubscribe from the backend to clear the existing push subscription (keys and endpoint)
const oldEndpoint = await unsubscribeToPushNotifications(userId); const oldEndpoint = await unsubscribeToPushNotifications(userId);
// Subscribe again to generate a fresh push subscription with updated keys and endpoint
await subscribeToPushNotifications(userId, currentSettings); await subscribeToPushNotifications(userId, currentSettings);
if (oldEndpoint) { if (oldEndpoint) {
@@ -96,7 +99,6 @@ export const verifyAndResubscribePushSubscription = async (
); );
} catch (error) { } catch (error) {
// Ignore errors when deleting old endpoint (it might not exist) // Ignore errors when deleting old endpoint (it might not exist)
// This is expected when the endpoint was already cleaned up
} }
} }