Skip to content

Commit 95f189f

Browse files
committed
fix(webpush): improve iOS push subscription endpoint cleanup
Signed-off-by: 0xsysr3ll <[email protected]>
1 parent 5978587 commit 95f189f

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/components/UserProfile/UserSettings/UserNotificationSettings/UserNotificationsWebPush/index.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,16 @@ const UserWebPushSettings = () => {
109109
// Deletes/disables corresponding push subscription from database
110110
const disablePushNotifications = async (endpoint?: string) => {
111111
try {
112-
await unsubscribeToPushNotifications(user?.id, endpoint);
112+
const unsubscribedEndpoint = await unsubscribeToPushNotifications(
113+
user?.id,
114+
endpoint
115+
);
113116

114117
// Delete from backend if endpoint is available
115-
if (subEndpoint) {
116-
await deletePushSubscriptionFromBackend(subEndpoint);
118+
const endpointToDelete = unsubscribedEndpoint || subEndpoint || endpoint;
119+
120+
if (endpointToDelete) {
121+
await deletePushSubscriptionFromBackend(endpointToDelete);
117122
}
118123

119124
localStorage.setItem('pushNotificationsEnabled', 'false');

src/utils/pushSubscriptionHelpers.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export const verifyPushSubscription = async (
5656
);
5757

5858
return expectedServerKey === currentServerKey && data.endpoint === endpoint;
59-
} catch {
59+
} catch (error) {
6060
return false;
6161
}
6262
};
@@ -74,11 +74,24 @@ export const verifyAndResubscribePushSubscription = async (
7474
if (currentSettings.enablePushRegistration) {
7575
try {
7676
// Unsubscribe from the backend to clear the existing push subscription (keys and endpoint)
77-
await unsubscribeToPushNotifications(userId);
77+
const oldEndpoint = await unsubscribeToPushNotifications(userId);
7878

7979
// Subscribe again to generate a fresh push subscription with updated keys and endpoint
8080
await subscribeToPushNotifications(userId, currentSettings);
8181

82+
if (oldEndpoint && userId) {
83+
try {
84+
await axios.delete(
85+
`/api/v1/user/${userId}/pushSubscription/${encodeURIComponent(
86+
oldEndpoint
87+
)}`
88+
);
89+
} catch (error) {
90+
// Ignore errors when deleting old endpoint (it might not exist)
91+
// This is expected when the endpoint was already cleaned up
92+
}
93+
}
94+
8295
return true;
8396
} catch (error) {
8497
throw new Error(`[SW] Resubscribe failed: ${error.message}`);
@@ -136,24 +149,26 @@ export const subscribeToPushNotifications = async (
136149
export const unsubscribeToPushNotifications = async (
137150
userId: number | undefined,
138151
endpoint?: string
139-
) => {
152+
): Promise<string | null> => {
140153
if (!('serviceWorker' in navigator) || !userId) {
141-
return;
154+
return null;
142155
}
143156

144157
try {
145158
const { subscription } = await getPushSubscription();
146159

147160
if (!subscription) {
148-
return false;
161+
return null;
149162
}
150163

151164
const { endpoint: currentEndpoint } = subscription.toJSON();
152165

153166
if (!endpoint || endpoint === currentEndpoint) {
154167
await subscription.unsubscribe();
155-
return true;
168+
return currentEndpoint ?? null;
156169
}
170+
171+
return null;
157172
} catch (error) {
158173
throw new Error(
159174
`Issue unsubscribing to push notifications: ${error.message}`

0 commit comments

Comments
 (0)