@cloudsignal/pwa-sdk 2.1.0 → 2.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -93,5 +93,3 @@ function isValidUUID(value) {
93
93
  }
94
94
 
95
95
  export { generateAuthHeaders, generateHMACSignature, isValidUUID, makeAuthenticatedRequest };
96
- //# sourceMappingURL=chunk-IQHSODT4.js.map
97
- //# sourceMappingURL=chunk-IQHSODT4.js.map
@@ -0,0 +1 @@
1
+ export { generateAuthHeaders, generateHMACSignature, isValidUUID, makeAuthenticatedRequest } from './chunk-NSF32IGO.js';
package/dist/index.cjs CHANGED
@@ -1150,6 +1150,14 @@ function markInstallationRegistered(organizationId, serviceId, registrationId) {
1150
1150
  setStorageItem(`install_id_${organizationId}_${serviceId}`, registrationId);
1151
1151
  return setStorageItem(key, true);
1152
1152
  }
1153
+ function getLastRegisteredIdentity(organizationId, serviceId) {
1154
+ const key = `last_identity_${organizationId}_${serviceId}`;
1155
+ return getStorageItem(key);
1156
+ }
1157
+ function setLastRegisteredIdentity(organizationId, serviceId, identity) {
1158
+ const key = `last_identity_${organizationId}_${serviceId}`;
1159
+ return setStorageItem(key, identity);
1160
+ }
1153
1161
  var IndexedDBStorage = class {
1154
1162
  constructor(dbName = "CloudSignalPWA", dbVersion = 1) {
1155
1163
  this.db = null;
@@ -1420,6 +1428,27 @@ var PushNotificationManager = class {
1420
1428
  this.onPermissionDenied?.();
1421
1429
  return null;
1422
1430
  }
1431
+ const incomingEmail = options.userEmail ?? null;
1432
+ const incomingUserId = options.userId ?? null;
1433
+ const stored = getLastRegisteredIdentity(this.organizationId, this.serviceId);
1434
+ if (stored) {
1435
+ const emailChanged = stored.email !== null && incomingEmail !== null && stored.email !== incomingEmail;
1436
+ const userIdChanged = stored.userId !== null && incomingUserId !== null && stored.userId !== incomingUserId;
1437
+ if (emailChanged || userIdChanged) {
1438
+ this.log(
1439
+ `Identity changed on this device (was ${stored.email || stored.userId}, now ${incomingEmail || incomingUserId}); dropping stale push subscription`
1440
+ );
1441
+ try {
1442
+ const stale = await this.serviceWorkerRegistration.pushManager.getSubscription();
1443
+ if (stale) {
1444
+ await stale.unsubscribe();
1445
+ this.pushSubscription = null;
1446
+ }
1447
+ } catch (err) {
1448
+ this.log(`Stale subscription unsubscribe failed: ${err}`, "error");
1449
+ }
1450
+ }
1451
+ }
1423
1452
  const subscription = await this.subscribeToPush();
1424
1453
  if (!subscription) {
1425
1454
  throw new Error("Failed to subscribe to push notifications");
@@ -1460,12 +1489,23 @@ var PushNotificationManager = class {
1460
1489
  this.onTokenExpired
1461
1490
  );
1462
1491
  if (!response.ok) {
1492
+ if (response.status === 422) {
1493
+ const body = await response.clone().json().catch(() => null);
1494
+ if (body?.detail?.code === "user_identity_required") {
1495
+ this.log("Skipping push registration: service requires user identity");
1496
+ return null;
1497
+ }
1498
+ }
1463
1499
  const errorText = await response.text();
1464
1500
  throw new Error(`Registration failed: ${response.status} - ${errorText}`);
1465
1501
  }
1466
1502
  const result = await response.json();
1467
1503
  this.registrationId = result.registration_id;
1468
1504
  setRegistrationId(this.organizationId, this.serviceId, this.registrationId);
1505
+ setLastRegisteredIdentity(this.organizationId, this.serviceId, {
1506
+ email: incomingEmail,
1507
+ userId: incomingUserId
1508
+ });
1469
1509
  const registration = {
1470
1510
  registrationId: result.registration_id,
1471
1511
  status: result.status || "active",
@@ -1526,6 +1566,13 @@ var PushNotificationManager = class {
1526
1566
  this.onTokenExpired
1527
1567
  );
1528
1568
  if (!response.ok) {
1569
+ if (response.status === 422) {
1570
+ const body = await response.clone().json().catch(() => null);
1571
+ if (body?.detail?.code === "user_identity_required") {
1572
+ this.log("Skipping install-only registration: service requires user identity");
1573
+ return null;
1574
+ }
1575
+ }
1529
1576
  const errorText = await response.text();
1530
1577
  throw new Error(`Installation registration failed: ${response.status} - ${errorText}`);
1531
1578
  }
@@ -4246,5 +4293,3 @@ exports.removeStorageItem = removeStorageItem;
4246
4293
  exports.setRegistrationId = setRegistrationId;
4247
4294
  exports.setStorageItem = setStorageItem;
4248
4295
  exports.updateAuthToken = updateAuthToken;
4249
- //# sourceMappingURL=index.cjs.map
4250
- //# sourceMappingURL=index.cjs.map