dexie-cloud-addon 4.2.5 → 4.3.2

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.
Files changed (170) hide show
  1. package/TODO-SOCIALAUTH.md +274 -0
  2. package/dist/modern/DexieCloudAPI.d.ts +4 -0
  3. package/dist/modern/DexieCloudOptions.d.ts +17 -0
  4. package/dist/modern/authentication/exchangeOAuthCode.d.ts +23 -0
  5. package/dist/modern/authentication/fetchAuthProviders.d.ts +14 -0
  6. package/dist/modern/authentication/handleOAuthCallback.d.ts +63 -0
  7. package/dist/modern/authentication/interactWithUser.d.ts +22 -0
  8. package/dist/modern/authentication/oauthLogin.d.ts +38 -0
  9. package/dist/modern/default-ui/AuthProviderButton.d.ts +21 -0
  10. package/dist/modern/default-ui/LoginDialog.d.ts +30 -2
  11. package/dist/modern/default-ui/OptionButton.d.ts +21 -0
  12. package/dist/modern/default-ui/ProviderSelectionDialog.d.ts +7 -0
  13. package/dist/modern/default-ui/SelectDialog.d.ts +10 -0
  14. package/dist/modern/dexie-cloud-addon.js +686 -13
  15. package/dist/modern/dexie-cloud-addon.js.map +1 -1
  16. package/dist/modern/dexie-cloud-addon.min.js +1 -1
  17. package/dist/modern/dexie-cloud-addon.min.js.gz +0 -0
  18. package/dist/modern/dexie-cloud-addon.min.js.map +1 -1
  19. package/dist/modern/errors/OAuthError.d.ts +10 -0
  20. package/dist/modern/service-worker.js +686 -13
  21. package/dist/modern/service-worker.js.map +1 -1
  22. package/dist/modern/service-worker.min.js +1 -1
  23. package/dist/modern/service-worker.min.js.map +1 -1
  24. package/dist/modern/types/DXCUserInteraction.d.ts +32 -1
  25. package/dist/umd/DISABLE_SERVICEWORKER_STRATEGY.d.ts +1 -0
  26. package/dist/umd/DXCWebSocketStatus.d.ts +1 -0
  27. package/dist/umd/DexieCloudAPI.d.ts +75 -0
  28. package/dist/umd/DexieCloudOptions.d.ts +27 -0
  29. package/dist/umd/DexieCloudSyncOptions.d.ts +4 -0
  30. package/dist/umd/DexieCloudTable.d.ts +18 -0
  31. package/dist/umd/InvalidLicenseError.d.ts +5 -0
  32. package/dist/umd/Invite.d.ts +8 -0
  33. package/dist/umd/PermissionChecker.d.ts +15 -0
  34. package/dist/umd/TSON.d.ts +17 -0
  35. package/dist/umd/WSObservable.d.ts +72 -0
  36. package/dist/umd/associate.d.ts +1 -0
  37. package/dist/umd/authentication/AuthPersistedContext.d.ts +9 -0
  38. package/dist/umd/authentication/TokenErrorResponseError.d.ts +10 -0
  39. package/dist/umd/authentication/TokenExpiredError.d.ts +3 -0
  40. package/dist/umd/authentication/UNAUTHORIZED_USER.d.ts +2 -0
  41. package/dist/umd/authentication/authenticate.d.ts +13 -0
  42. package/dist/umd/authentication/currentUserObservable.d.ts +1 -0
  43. package/dist/umd/authentication/interactWithUser.d.ts +21 -0
  44. package/dist/umd/authentication/login.d.ts +3 -0
  45. package/dist/umd/authentication/logout.d.ts +5 -0
  46. package/dist/umd/authentication/otpFetchTokenCallback.d.ts +3 -0
  47. package/dist/umd/authentication/setCurrentUser.d.ts +14 -0
  48. package/dist/umd/authentication/waitUntil.d.ts +3 -0
  49. package/dist/umd/computeSyncState.d.ts +4 -0
  50. package/dist/umd/createSharedValueObservable.d.ts +3 -0
  51. package/dist/umd/currentUserEmitter.d.ts +3 -0
  52. package/dist/umd/db/DexieCloudDB.d.ts +61 -0
  53. package/dist/umd/db/entities/BaseRevisionMapEntry.d.ts +5 -0
  54. package/dist/umd/db/entities/EntityCommon.d.ts +5 -0
  55. package/dist/umd/db/entities/GuardedJob.d.ts +5 -0
  56. package/dist/umd/db/entities/Member.d.ts +19 -0
  57. package/dist/umd/db/entities/PersistedSyncState.d.ts +22 -0
  58. package/dist/umd/db/entities/Realm.d.ts +14 -0
  59. package/dist/umd/db/entities/Role.d.ts +11 -0
  60. package/dist/umd/db/entities/UserLogin.d.ts +23 -0
  61. package/dist/umd/default-ui/Dialog.d.ts +5 -0
  62. package/dist/umd/default-ui/LoginDialog.d.ts +3 -0
  63. package/dist/umd/default-ui/Styles.d.ts +3 -0
  64. package/dist/umd/default-ui/index.d.ts +24 -0
  65. package/dist/umd/define-ydoc-trigger.d.ts +3 -0
  66. package/dist/umd/dexie-cloud-addon.d.ts +3 -0
  67. package/dist/umd/dexie-cloud-addon.js +687 -14
  68. package/dist/umd/dexie-cloud-addon.js.gz +0 -0
  69. package/dist/umd/dexie-cloud-addon.js.map +1 -1
  70. package/dist/umd/dexie-cloud-addon.min.js +1 -1
  71. package/dist/umd/dexie-cloud-addon.min.js.gz +0 -0
  72. package/dist/umd/dexie-cloud-addon.min.js.map +1 -1
  73. package/dist/umd/dexie-cloud-client.d.ts +23 -0
  74. package/dist/umd/errors/HttpError.d.ts +5 -0
  75. package/dist/umd/extend-dexie-interface.d.ts +23 -0
  76. package/dist/umd/getGlobalRolesObservable.d.ts +5 -0
  77. package/dist/umd/getInternalAccessControlObservable.d.ts +12 -0
  78. package/dist/umd/getInvitesObservable.d.ts +23 -0
  79. package/dist/umd/getPermissionsLookupObservable.d.ts +16 -0
  80. package/dist/umd/getTiedRealmId.d.ts +2 -0
  81. package/dist/umd/helpers/BroadcastedAndLocalEvent.d.ts +8 -0
  82. package/dist/umd/helpers/CancelToken.d.ts +4 -0
  83. package/dist/umd/helpers/IS_SERVICE_WORKER.d.ts +1 -0
  84. package/dist/umd/helpers/SWBroadcastChannel.d.ts +12 -0
  85. package/dist/umd/helpers/allSettled.d.ts +1 -0
  86. package/dist/umd/helpers/bulkUpdate.d.ts +4 -0
  87. package/dist/umd/helpers/computeRealmSetHash.d.ts +2 -0
  88. package/dist/umd/helpers/date-constants.d.ts +5 -0
  89. package/dist/umd/helpers/flatten.d.ts +1 -0
  90. package/dist/umd/helpers/getMutationTable.d.ts +1 -0
  91. package/dist/umd/helpers/getSyncableTables.d.ts +4 -0
  92. package/dist/umd/helpers/getTableFromMutationTable.d.ts +1 -0
  93. package/dist/umd/helpers/makeArray.d.ts +1 -0
  94. package/dist/umd/helpers/randomString.d.ts +1 -0
  95. package/dist/umd/helpers/resolveText.d.ts +16 -0
  96. package/dist/umd/helpers/throwVersionIncrementNeeded.d.ts +1 -0
  97. package/dist/umd/helpers/visibilityState.d.ts +1 -0
  98. package/dist/umd/isEagerSyncDisabled.d.ts +2 -0
  99. package/dist/umd/isFirefox.d.ts +1 -0
  100. package/dist/umd/isSafari.d.ts +2 -0
  101. package/dist/umd/mapValueObservable.d.ts +5 -0
  102. package/dist/umd/mergePermissions.d.ts +2 -0
  103. package/dist/umd/middleware-helpers/guardedTable.d.ts +11 -0
  104. package/dist/umd/middleware-helpers/idGenerationHelpers.d.ts +18 -0
  105. package/dist/umd/middlewares/createIdGenerationMiddleware.d.ts +3 -0
  106. package/dist/umd/middlewares/createImplicitPropSetterMiddleware.d.ts +3 -0
  107. package/dist/umd/middlewares/createMutationTrackingMiddleware.d.ts +17 -0
  108. package/dist/umd/middlewares/outstandingTransaction.d.ts +4 -0
  109. package/dist/umd/overrideParseStoresSpec.d.ts +4 -0
  110. package/dist/umd/performInitialSync.d.ts +4 -0
  111. package/dist/umd/permissions.d.ts +9 -0
  112. package/dist/umd/prodLog.d.ts +9 -0
  113. package/dist/umd/service-worker.d.ts +1 -0
  114. package/dist/umd/service-worker.js +687 -14
  115. package/dist/umd/service-worker.js.map +1 -1
  116. package/dist/umd/service-worker.min.js +1 -1
  117. package/dist/umd/service-worker.min.js.map +1 -1
  118. package/dist/umd/sync/DEXIE_CLOUD_SYNCER_ID.d.ts +1 -0
  119. package/dist/umd/sync/LocalSyncWorker.d.ts +7 -0
  120. package/dist/umd/sync/SyncRequiredError.d.ts +3 -0
  121. package/dist/umd/sync/applyServerChanges.d.ts +3 -0
  122. package/dist/umd/sync/connectWebSocket.d.ts +2 -0
  123. package/dist/umd/sync/encodeIdsForServer.d.ts +4 -0
  124. package/dist/umd/sync/extractRealm.d.ts +2 -0
  125. package/dist/umd/sync/getLatestRevisionsPerTable.d.ts +6 -0
  126. package/dist/umd/sync/getTablesToSyncify.d.ts +3 -0
  127. package/dist/umd/sync/isOnline.d.ts +1 -0
  128. package/dist/umd/sync/isSyncNeeded.d.ts +2 -0
  129. package/dist/umd/sync/listClientChanges.d.ts +9 -0
  130. package/dist/umd/sync/listSyncifiedChanges.d.ts +5 -0
  131. package/dist/umd/sync/messageConsumerIsReady.d.ts +2 -0
  132. package/dist/umd/sync/messagesFromServerQueue.d.ts +8 -0
  133. package/dist/umd/sync/modifyLocalObjectsWithNewUserId.d.ts +4 -0
  134. package/dist/umd/sync/myId.d.ts +1 -0
  135. package/dist/umd/sync/numUnsyncedMutations.d.ts +2 -0
  136. package/dist/umd/sync/old_startSyncingClientChanges.d.ts +39 -0
  137. package/dist/umd/sync/performGuardedJob.d.ts +2 -0
  138. package/dist/umd/sync/ratelimit.d.ts +3 -0
  139. package/dist/umd/sync/registerSyncEvent.d.ts +3 -0
  140. package/dist/umd/sync/sync.d.ts +15 -0
  141. package/dist/umd/sync/syncIfPossible.d.ts +5 -0
  142. package/dist/umd/sync/syncWithServer.d.ts +6 -0
  143. package/dist/umd/sync/triggerSync.d.ts +2 -0
  144. package/dist/umd/sync/updateBaseRevs.d.ts +5 -0
  145. package/dist/umd/types/DXCAlert.d.ts +25 -0
  146. package/dist/umd/types/DXCInputField.d.ts +11 -0
  147. package/dist/umd/types/DXCUserInteraction.d.ts +93 -0
  148. package/dist/umd/types/NewIdOptions.d.ts +3 -0
  149. package/dist/umd/types/SWMessageEvent.d.ts +3 -0
  150. package/dist/umd/types/SWSyncEvent.d.ts +4 -0
  151. package/dist/umd/types/SyncState.d.ts +9 -0
  152. package/dist/umd/types/TXExpandos.d.ts +11 -0
  153. package/dist/umd/updateSchemaFromOptions.d.ts +3 -0
  154. package/dist/umd/userIsActive.d.ts +7 -0
  155. package/dist/umd/verifyConfig.d.ts +2 -0
  156. package/dist/umd/verifySchema.d.ts +2 -0
  157. package/dist/umd/yjs/YDexieCloudSyncState.d.ts +3 -0
  158. package/dist/umd/yjs/YTable.d.ts +3 -0
  159. package/dist/umd/yjs/applyYMessages.d.ts +9 -0
  160. package/dist/umd/yjs/awareness.d.ts +3 -0
  161. package/dist/umd/yjs/createYClientUpdateObservable.d.ts +4 -0
  162. package/dist/umd/yjs/createYHandler.d.ts +2 -0
  163. package/dist/umd/yjs/downloadYDocsFromServer.d.ts +3 -0
  164. package/dist/umd/yjs/getUpdatesTable.d.ts +3 -0
  165. package/dist/umd/yjs/listUpdatesSince.d.ts +3 -0
  166. package/dist/umd/yjs/listYClientMessagesAndStateVector.d.ts +26 -0
  167. package/dist/umd/yjs/reopenDocSignal.d.ts +10 -0
  168. package/dist/umd/yjs/updateYSyncStates.d.ts +6 -0
  169. package/oauth_flow.md +307 -0
  170. package/package.json +1 -1
@@ -8,7 +8,7 @@
8
8
  *
9
9
  * ==========================================================================
10
10
  *
11
- * Version 4.2.5, Sat Dec 20 2025
11
+ * Version 4.3.2, Thu Jan 22 2026
12
12
  *
13
13
  * https://dexie.org
14
14
  *
@@ -761,6 +761,74 @@ class TokenErrorResponseError extends Error {
761
761
  }
762
762
  }
763
763
 
764
+ /** Cache for fetched SVG content to avoid re-fetching */
765
+ const svgCache = {};
766
+ /** Default SVG icons for built-in OAuth providers */
767
+ const ProviderIcons = {
768
+ google: `<svg viewBox="0 0 24 24" width="20" height="20"><path fill="#4285F4" d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"/><path fill="#34A853" d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"/><path fill="#FBBC05" d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"/><path fill="#EA4335" d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"/></svg>`,
769
+ github: `<svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor"><path d="M12 0C5.37 0 0 5.37 0 12c0 5.31 3.435 9.795 8.205 11.385.6.105.825-.255.825-.57 0-.285-.015-1.23-.015-2.235-3.015.555-3.795-.735-4.035-1.41-.135-.345-.72-1.41-1.23-1.695-.42-.225-1.02-.78-.015-.795.945-.015 1.62.87 1.845 1.23 1.08 1.815 2.805 1.305 3.495.99.105-.78.42-1.305.765-1.605-2.67-.3-5.46-1.335-5.46-5.925 0-1.305.465-2.385 1.23-3.225-.12-.3-.54-1.53.12-3.18 0 0 1.005-.315 3.3 1.23.96-.27 1.98-.405 3-.405s2.04.135 3 .405c2.295-1.56 3.3-1.23 3.3-1.23.66 1.65.24 2.88.12 3.18.765.84 1.23 1.905 1.23 3.225 0 4.605-2.805 5.625-5.475 5.925.435.375.81 1.095.81 2.22 0 1.605-.015 2.895-.015 3.3 0 .315.225.69.825.57A12.02 12.02 0 0024 12c0-6.63-5.37-12-12-12z"/></svg>`,
770
+ microsoft: `<svg viewBox="0 0 24 24" width="20" height="20"><rect fill="#F25022" x="1" y="1" width="10" height="10"/><rect fill="#00A4EF" x="1" y="13" width="10" height="10"/><rect fill="#7FBA00" x="13" y="1" width="10" height="10"/><rect fill="#FFB900" x="13" y="13" width="10" height="10"/></svg>`,
771
+ apple: `<svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor"><path d="M18.71 19.5c-.83 1.24-1.71 2.45-3.05 2.47-1.34.03-1.77-.79-3.29-.79-1.53 0-2 .77-3.27.82-1.31.05-2.3-1.32-3.14-2.53C4.25 17 2.94 12.45 4.7 9.39c.87-1.52 2.43-2.48 4.12-2.51 1.28-.02 2.5.87 3.29.87.78 0 2.26-1.07 3.81-.91.65.03 2.47.26 3.64 1.98-.09.06-2.17 1.28-2.15 3.81.03 3.02 2.65 4.03 2.68 4.04-.03.07-.42 1.44-1.38 2.83M13 3.5c.73-.83 1.94-1.46 2.94-1.5.13 1.17-.34 2.35-1.04 3.19-.69.85-1.83 1.51-2.95 1.42-.15-1.15.41-2.35 1.05-3.11z"/></svg>`,
772
+ };
773
+ /** Email/envelope icon for OTP option */
774
+ const EmailIcon = `<svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="2"><rect x="2" y="4" width="20" height="16" rx="2"/><path d="M22 6L12 13 2 6"/></svg>`;
775
+ /**
776
+ * Fetches SVG content from a URL and caches it.
777
+ * Returns the SVG string or null if fetch fails.
778
+ */
779
+ function fetchSvgIcon(url) {
780
+ return __awaiter(this, void 0, void 0, function* () {
781
+ if (svgCache[url]) {
782
+ return svgCache[url];
783
+ }
784
+ try {
785
+ const res = yield fetch(url);
786
+ if (res.ok) {
787
+ const svg = yield res.text();
788
+ // Validate it looks like SVG
789
+ if (svg.includes('<svg')) {
790
+ svgCache[url] = svg;
791
+ return svg;
792
+ }
793
+ }
794
+ }
795
+ catch (_a) {
796
+ // Silently fail - will show no icon
797
+ }
798
+ return null;
799
+ });
800
+ }
801
+ /**
802
+ * Converts an OAuthProviderInfo to a generic DXCOption.
803
+ * Fetches SVG icons from URLs if needed.
804
+ */
805
+ function providerToOption(provider) {
806
+ return __awaiter(this, void 0, void 0, function* () {
807
+ var _a;
808
+ let iconSvg;
809
+ // First check for built-in icons
810
+ if (ProviderIcons[provider.type]) {
811
+ iconSvg = ProviderIcons[provider.type];
812
+ }
813
+ // If provider has iconUrl pointing to SVG, fetch and inline it
814
+ else if ((_a = provider.iconUrl) === null || _a === void 0 ? void 0 : _a.toLowerCase().endsWith('.svg')) {
815
+ const fetched = yield fetchSvgIcon(provider.iconUrl);
816
+ if (fetched) {
817
+ iconSvg = fetched;
818
+ }
819
+ }
820
+ return {
821
+ name: 'provider',
822
+ value: provider.name,
823
+ displayName: `Continue with ${provider.displayName}`,
824
+ iconSvg,
825
+ // If iconUrl is not SVG, pass it through for img tag rendering
826
+ iconUrl: (!iconSvg && provider.iconUrl) ? provider.iconUrl : undefined,
827
+ // Use provider type as style hint for branding
828
+ styleHint: provider.type,
829
+ };
830
+ });
831
+ }
764
832
  function interactWithUser(userInteraction, req) {
765
833
  return new Promise((resolve, reject) => {
766
834
  const interactionProps = Object.assign(Object.assign({ submitLabel: 'Submit', cancelLabel: 'Cancel' }, req), { onSubmit: (res) => {
@@ -895,6 +963,67 @@ function confirmLogout(userInteraction, currentUserId, numUnsyncedChanges) {
895
963
  .catch(() => false);
896
964
  });
897
965
  }
966
+ /**
967
+ * Prompts the user to select an authentication method (OAuth provider or OTP).
968
+ *
969
+ * This function converts OAuth providers and OTP option into generic DXCOption[]
970
+ * for the DXCSelect interaction, handling icon fetching and style hints.
971
+ *
972
+ * @param userInteraction - The user interaction BehaviorSubject
973
+ * @param providers - Available OAuth providers
974
+ * @param otpEnabled - Whether OTP is available
975
+ * @param title - Dialog title
976
+ * @param alerts - Optional alerts to display
977
+ * @returns Promise resolving to the user's selection
978
+ */
979
+ function promptForProvider(userInteraction_1, providers_1, otpEnabled_1) {
980
+ return __awaiter(this, arguments, void 0, function* (userInteraction, providers, otpEnabled, title = 'Choose login method', alerts = []) {
981
+ // Convert providers to generic options (with icon fetching)
982
+ const providerOptions = yield Promise.all(providers.map(providerToOption));
983
+ // Build the options array
984
+ const options = [...providerOptions];
985
+ // Add OTP option if enabled
986
+ if (otpEnabled) {
987
+ options.push({
988
+ name: 'otp',
989
+ value: 'email',
990
+ displayName: 'Continue with email',
991
+ iconSvg: EmailIcon,
992
+ styleHint: 'otp',
993
+ });
994
+ }
995
+ return new Promise((resolve, reject) => {
996
+ const interactionProps = {
997
+ type: 'generic',
998
+ title,
999
+ alerts,
1000
+ options,
1001
+ fields: {},
1002
+ submitLabel: '', // No submit button - just options
1003
+ cancelLabel: 'Cancel',
1004
+ onSubmit: (params) => {
1005
+ userInteraction.next(undefined);
1006
+ // Check which option was selected
1007
+ if ('otp' in params) {
1008
+ resolve({ type: 'otp' });
1009
+ }
1010
+ else if ('provider' in params) {
1011
+ resolve({ type: 'provider', provider: params.provider });
1012
+ }
1013
+ else {
1014
+ // Unknown - default to OTP
1015
+ resolve({ type: 'otp' });
1016
+ }
1017
+ },
1018
+ onCancel: () => {
1019
+ userInteraction.next(undefined);
1020
+ reject(new Dexie.AbortError('User cancelled'));
1021
+ },
1022
+ };
1023
+ userInteraction.next(interactionProps);
1024
+ });
1025
+ });
1026
+ }
898
1027
 
899
1028
  function loadAccessToken(db) {
900
1029
  return __awaiter(this, void 0, void 0, function* () {
@@ -1218,15 +1347,215 @@ class HttpError extends Error {
1218
1347
  }
1219
1348
  }
1220
1349
 
1350
+ /** User-friendly messages for OAuth error codes */
1351
+ const ERROR_MESSAGES = {
1352
+ access_denied: 'Access was denied by the authentication provider.',
1353
+ invalid_state: 'The authentication response could not be verified. Please try again.',
1354
+ email_not_verified: 'Your email address must be verified before you can log in.',
1355
+ expired_code: 'The authentication code has expired. Please try again.',
1356
+ provider_error: 'An error occurred with the authentication provider.',
1357
+ network_error: 'A network error occurred during authentication. Please check your connection and try again.',
1358
+ };
1359
+ /** Error class for OAuth-specific errors */
1360
+ class OAuthError extends Error {
1361
+ constructor(code, provider, customMessage) {
1362
+ super(customMessage || ERROR_MESSAGES[code]);
1363
+ this.name = 'OAuthError';
1364
+ this.code = code;
1365
+ this.provider = provider;
1366
+ }
1367
+ /** Get user-friendly message for this error */
1368
+ get userMessage() {
1369
+ return ERROR_MESSAGES[this.code] || this.message;
1370
+ }
1371
+ }
1372
+
1373
+ /**
1374
+ * Exchanges a Dexie Cloud authorization code for access and refresh tokens.
1375
+ *
1376
+ * This is called after the OAuth callback delivers the authorization code
1377
+ * via postMessage (popup flow) or redirect.
1378
+ *
1379
+ * @param options - Exchange options
1380
+ * @returns Promise resolving to TokenFinalResponse
1381
+ * @throws OAuthError or TokenErrorResponseError on failure
1382
+ */
1383
+ function exchangeOAuthCode(options) {
1384
+ return __awaiter(this, void 0, void 0, function* () {
1385
+ const { databaseUrl, code, publicKey, scopes = ['ACCESS_DB'] } = options;
1386
+ const tokenRequest = {
1387
+ grant_type: 'authorization_code',
1388
+ code,
1389
+ public_key: publicKey,
1390
+ scopes,
1391
+ };
1392
+ try {
1393
+ const res = yield fetch(`${databaseUrl}/token`, {
1394
+ method: 'POST',
1395
+ headers: { 'Content-Type': 'application/json' },
1396
+ body: JSON.stringify(tokenRequest),
1397
+ mode: 'cors',
1398
+ });
1399
+ if (!res.ok) {
1400
+ if (res.status === 400 || res.status === 401) {
1401
+ // Try to parse error response
1402
+ try {
1403
+ const errorResponse = yield res.json();
1404
+ if (errorResponse.type === 'error') {
1405
+ // Check for specific error codes
1406
+ if (errorResponse.messageCode === 'INVALID_OTP') {
1407
+ // In the context of OAuth, this likely means expired code
1408
+ throw new OAuthError('expired_code', undefined, errorResponse.message);
1409
+ }
1410
+ throw new TokenErrorResponseError(errorResponse);
1411
+ }
1412
+ }
1413
+ catch (e) {
1414
+ if (e instanceof OAuthError || e instanceof TokenErrorResponseError) {
1415
+ throw e;
1416
+ }
1417
+ // Fall through to generic error
1418
+ }
1419
+ }
1420
+ const errorText = yield res.text().catch(() => res.statusText);
1421
+ throw new OAuthError('provider_error', undefined, `Token exchange failed: ${res.status} ${errorText}`);
1422
+ }
1423
+ const response = yield res.json();
1424
+ if (response.type === 'error') {
1425
+ throw new TokenErrorResponseError(response);
1426
+ }
1427
+ if (response.type !== 'tokens') {
1428
+ throw new OAuthError('provider_error', undefined, `Unexpected response type: ${response.type}`);
1429
+ }
1430
+ return response;
1431
+ }
1432
+ catch (error) {
1433
+ if (error instanceof OAuthError || error instanceof TokenErrorResponseError) {
1434
+ throw error;
1435
+ }
1436
+ if (error instanceof TypeError) {
1437
+ // Network error
1438
+ throw new OAuthError('network_error');
1439
+ }
1440
+ throw error;
1441
+ }
1442
+ });
1443
+ }
1444
+
1445
+ /** Default response when OAuth is disabled or unavailable */
1446
+ const OTP_ONLY_RESPONSE = {
1447
+ providers: [],
1448
+ otpEnabled: true,
1449
+ };
1450
+ /**
1451
+ * Fetches available authentication providers from the Dexie Cloud server.
1452
+ *
1453
+ * @param databaseUrl - The Dexie Cloud database URL
1454
+ * @param socialAuthEnabled - Whether social auth is enabled in client config (default: true)
1455
+ * @returns Promise resolving to AuthProvidersResponse
1456
+ *
1457
+ * Handles failures gracefully:
1458
+ * - 404 → Returns OTP-only (old server version)
1459
+ * - Network error → Returns OTP-only
1460
+ * - socialAuthEnabled: false → Returns OTP-only without fetching
1461
+ */
1462
+ function fetchAuthProviders(databaseUrl_1) {
1463
+ return __awaiter(this, arguments, void 0, function* (databaseUrl, socialAuthEnabled = true) {
1464
+ // If social auth is disabled, return OTP-only without fetching
1465
+ if (!socialAuthEnabled) {
1466
+ return OTP_ONLY_RESPONSE;
1467
+ }
1468
+ try {
1469
+ const res = yield fetch(`${databaseUrl}/auth-providers`, {
1470
+ method: 'GET',
1471
+ headers: { 'Accept': 'application/json' },
1472
+ mode: 'cors',
1473
+ });
1474
+ if (res.status === 404) {
1475
+ // Old server version without OAuth support
1476
+ console.debug('[dexie-cloud] Server does not support /auth-providers endpoint. Using OTP-only authentication.');
1477
+ return OTP_ONLY_RESPONSE;
1478
+ }
1479
+ if (!res.ok) {
1480
+ console.warn(`[dexie-cloud] Failed to fetch auth providers: ${res.status} ${res.statusText}`);
1481
+ return OTP_ONLY_RESPONSE;
1482
+ }
1483
+ return yield res.json();
1484
+ }
1485
+ catch (error) {
1486
+ // Network error or other failure - fall back to OTP
1487
+ console.debug('[dexie-cloud] Could not fetch auth providers, falling back to OTP:', error);
1488
+ return OTP_ONLY_RESPONSE;
1489
+ }
1490
+ });
1491
+ }
1492
+
1493
+ /** Build the OAuth login URL */
1494
+ function buildOAuthLoginUrl(options) {
1495
+ const url = new URL(`${options.databaseUrl}/oauth/login/${options.provider}`);
1496
+ // Set the redirect URI - defaults to current page URL for web SPAs
1497
+ const redirectUri = options.redirectUri ||
1498
+ (typeof window !== 'undefined' ? window.location.href : '');
1499
+ if (redirectUri) {
1500
+ url.searchParams.set('redirect_uri', redirectUri);
1501
+ }
1502
+ return url.toString();
1503
+ }
1504
+ /**
1505
+ * Initiates OAuth login via full page redirect.
1506
+ *
1507
+ * The page will navigate to the OAuth provider. After authentication,
1508
+ * the user is redirected back to the app with a `dxc-auth` query parameter
1509
+ * containing base64url-encoded JSON with the authorization code.
1510
+ *
1511
+ * The dexie-cloud-addon automatically detects and processes this parameter
1512
+ * when db.cloud.configure() is called on page load.
1513
+ *
1514
+ * @param options - OAuth redirect options
1515
+ *
1516
+ * @example
1517
+ * ```typescript
1518
+ * // Initiate OAuth login
1519
+ * startOAuthRedirect({
1520
+ * databaseUrl: 'https://mydb.dexie.cloud',
1521
+ * provider: 'google'
1522
+ * });
1523
+ * // Page navigates away, user authenticates, then returns with auth code
1524
+ * ```
1525
+ */
1526
+ function startOAuthRedirect(options) {
1527
+ // Store provider in sessionStorage for reference on callback
1528
+ if (typeof sessionStorage !== 'undefined') {
1529
+ sessionStorage.setItem('dexie-cloud-oauth-provider', options.provider);
1530
+ }
1531
+ const loginUrl = buildOAuthLoginUrl(options);
1532
+ window.location.href = loginUrl;
1533
+ }
1534
+
1221
1535
  function otpFetchTokenCallback(db) {
1222
1536
  const { userInteraction } = db.cloud;
1223
1537
  return function otpAuthenticate(_a) {
1224
1538
  return __awaiter(this, arguments, void 0, function* ({ public_key, hints }) {
1225
- var _b;
1539
+ var _b, _c;
1226
1540
  let tokenRequest;
1227
1541
  const url = (_b = db.cloud.options) === null || _b === void 0 ? void 0 : _b.databaseUrl;
1228
1542
  if (!url)
1229
1543
  throw new Error(`No database URL given.`);
1544
+ // Handle OAuth code exchange (from redirect/deep link flows)
1545
+ if ((hints === null || hints === void 0 ? void 0 : hints.oauthCode) && hints.provider) {
1546
+ return yield exchangeOAuthCode({
1547
+ databaseUrl: url,
1548
+ code: hints.oauthCode,
1549
+ publicKey: public_key,
1550
+ scopes: ['ACCESS_DB'],
1551
+ });
1552
+ }
1553
+ // Handle OAuth provider login via redirect
1554
+ if (hints === null || hints === void 0 ? void 0 : hints.provider) {
1555
+ initiateOAuthRedirect(db, hints.provider);
1556
+ // This function never returns - page navigates away
1557
+ throw new Error('OAuth redirect initiated');
1558
+ }
1230
1559
  if ((hints === null || hints === void 0 ? void 0 : hints.grant_type) === 'demo') {
1231
1560
  const demo_user = yield promptForEmail(userInteraction, 'Enter a demo user email', (hints === null || hints === void 0 ? void 0 : hints.email) || (hints === null || hints === void 0 ? void 0 : hints.userId));
1232
1561
  tokenRequest = {
@@ -1250,6 +1579,20 @@ function otpFetchTokenCallback(db) {
1250
1579
  };
1251
1580
  }
1252
1581
  else {
1582
+ // Check for available auth providers (OAuth + OTP)
1583
+ const socialAuthEnabled = ((_c = db.cloud.options) === null || _c === void 0 ? void 0 : _c.socialAuth) !== false;
1584
+ const authProviders = yield fetchAuthProviders(url, socialAuthEnabled);
1585
+ // If we have OAuth providers available, prompt for selection
1586
+ if (authProviders.providers.length > 0) {
1587
+ const selection = yield promptForProvider(userInteraction, authProviders.providers, authProviders.otpEnabled, 'Sign in');
1588
+ if (selection.type === 'provider') {
1589
+ // User selected an OAuth provider - initiate redirect
1590
+ initiateOAuthRedirect(db, selection.provider);
1591
+ // This function never returns - page navigates away
1592
+ throw new Error('OAuth redirect initiated');
1593
+ }
1594
+ // User chose OTP - continue with email prompt below
1595
+ }
1253
1596
  const email = yield promptForEmail(userInteraction, 'Enter email address', hints === null || hints === void 0 ? void 0 : hints.email);
1254
1597
  if (/@demo.local$/.test(email)) {
1255
1598
  tokenRequest = {
@@ -1327,6 +1670,27 @@ function otpFetchTokenCallback(db) {
1327
1670
  });
1328
1671
  };
1329
1672
  }
1673
+ /**
1674
+ * Initiates OAuth login via full page redirect.
1675
+ *
1676
+ * The page will navigate away to the OAuth provider. After authentication,
1677
+ * the user is redirected back with a dxc-auth query parameter that is
1678
+ * automatically detected by db.cloud.configure().
1679
+ */
1680
+ function initiateOAuthRedirect(db, provider) {
1681
+ var _a, _b;
1682
+ const url = (_a = db.cloud.options) === null || _a === void 0 ? void 0 : _a.databaseUrl;
1683
+ if (!url)
1684
+ throw new Error(`No database URL given.`);
1685
+ const redirectUri = ((_b = db.cloud.options) === null || _b === void 0 ? void 0 : _b.oauthRedirectUri) ||
1686
+ (typeof window !== 'undefined' ? window.location.href : undefined);
1687
+ // Start OAuth redirect flow - page navigates away
1688
+ startOAuthRedirect({
1689
+ databaseUrl: url,
1690
+ provider,
1691
+ redirectUri,
1692
+ });
1693
+ }
1330
1694
 
1331
1695
  /** A way to log to console in production without terser stripping out
1332
1696
  * it from the release bundle.
@@ -5388,8 +5752,97 @@ const Styles = {
5388
5752
  color: "#333",
5389
5753
  borderBottom: "1px solid #eee",
5390
5754
  paddingBottom: "10px"
5391
- }
5392
- };
5755
+ },
5756
+ // OAuth Provider Button Styles
5757
+ ProviderButton: {
5758
+ display: "flex",
5759
+ alignItems: "center",
5760
+ justifyContent: "center",
5761
+ width: "100%",
5762
+ padding: "12px 16px",
5763
+ marginBottom: "10px",
5764
+ border: "1px solid #d1d5db",
5765
+ borderRadius: "6px",
5766
+ backgroundColor: "#ffffff",
5767
+ cursor: "pointer",
5768
+ fontSize: "14px",
5769
+ fontWeight: "500",
5770
+ color: "#374151",
5771
+ transition: "all 0.2s ease",
5772
+ gap: "12px"
5773
+ },
5774
+ ProviderButtonIcon: {
5775
+ width: "20px",
5776
+ height: "20px",
5777
+ flexShrink: 0,
5778
+ display: "flex",
5779
+ alignItems: "center",
5780
+ justifyContent: "center"
5781
+ },
5782
+ ProviderButtonText: {
5783
+ flex: 1,
5784
+ textAlign: "left"
5785
+ },
5786
+ // Provider-specific colors
5787
+ ProviderGoogle: {
5788
+ backgroundColor: "#ffffff",
5789
+ border: "1px solid #dadce0",
5790
+ color: "#3c4043"
5791
+ },
5792
+ ProviderGitHub: {
5793
+ backgroundColor: "#24292e",
5794
+ border: "1px solid #24292e",
5795
+ color: "#ffffff"
5796
+ },
5797
+ ProviderMicrosoft: {
5798
+ backgroundColor: "#ffffff",
5799
+ border: "1px solid #8c8c8c",
5800
+ color: "#5e5e5e"
5801
+ },
5802
+ ProviderApple: {
5803
+ backgroundColor: "#000000",
5804
+ border: "1px solid #000000",
5805
+ color: "#ffffff"
5806
+ },
5807
+ ProviderCustom: {
5808
+ backgroundColor: "#4f46e5",
5809
+ border: "1px solid #4f46e5",
5810
+ color: "#ffffff"
5811
+ },
5812
+ // Divider styles
5813
+ Divider: {
5814
+ display: "flex",
5815
+ alignItems: "center",
5816
+ margin: "20px 0",
5817
+ color: "#6b7280",
5818
+ fontSize: "13px"
5819
+ },
5820
+ DividerLine: {
5821
+ flex: 1,
5822
+ height: "1px",
5823
+ backgroundColor: "#e5e7eb"
5824
+ },
5825
+ DividerText: {
5826
+ padding: "0 12px",
5827
+ color: "#9ca3af"
5828
+ },
5829
+ // OTP Button (Continue with email)
5830
+ OtpButton: {
5831
+ display: "flex",
5832
+ alignItems: "center",
5833
+ justifyContent: "center",
5834
+ width: "100%",
5835
+ padding: "12px 16px",
5836
+ border: "1px solid #d1d5db",
5837
+ borderRadius: "6px",
5838
+ backgroundColor: "#f9fafb",
5839
+ cursor: "pointer",
5840
+ fontSize: "14px",
5841
+ fontWeight: "500",
5842
+ color: "#374151",
5843
+ transition: "all 0.2s ease",
5844
+ gap: "12px"
5845
+ }};
5393
5846
 
5394
5847
  function Dialog({ children, className }) {
5395
5848
  return (_$1("div", { className: `dexie-dialog ${className || ''}` },
@@ -5418,19 +5871,126 @@ function resolveText({ message, messageCode, messageParams }) {
5418
5871
  return message.replace(/\{\w+\}/ig, n => messageParams[n.substring(1, n.length - 1)]);
5419
5872
  }
5420
5873
 
5874
+ /** Get style based on styleHint (for provider branding, etc.) */
5875
+ function getOptionStyle(styleHint) {
5876
+ const baseStyle = Object.assign({}, Styles.ProviderButton);
5877
+ if (!styleHint) {
5878
+ return baseStyle;
5879
+ }
5880
+ switch (styleHint) {
5881
+ case 'google':
5882
+ return Object.assign(Object.assign({}, baseStyle), Styles.ProviderGoogle);
5883
+ case 'github':
5884
+ return Object.assign(Object.assign({}, baseStyle), Styles.ProviderGitHub);
5885
+ case 'microsoft':
5886
+ return Object.assign(Object.assign({}, baseStyle), Styles.ProviderMicrosoft);
5887
+ case 'apple':
5888
+ return Object.assign(Object.assign({}, baseStyle), Styles.ProviderApple);
5889
+ case 'otp':
5890
+ return Object.assign({}, Styles.OtpButton);
5891
+ case 'custom-oauth2':
5892
+ return Object.assign(Object.assign({}, baseStyle), Styles.ProviderCustom);
5893
+ default:
5894
+ return baseStyle;
5895
+ }
5896
+ }
5897
+ /**
5898
+ * Generic button component for selectable options.
5899
+ * Displays the option's icon and display name.
5900
+ *
5901
+ * The icon can be:
5902
+ * - Inline SVG (iconSvg) - rendered directly with dangerouslySetInnerHTML
5903
+ * - Image URL (iconUrl) - rendered as an img tag
5904
+ *
5905
+ * Style is determined by the styleHint property for branding purposes.
5906
+ */
5907
+ function OptionButton({ option, onClick }) {
5908
+ const { displayName, iconUrl, iconSvg, styleHint, value } = option;
5909
+ const style = getOptionStyle(styleHint);
5910
+ // Get the text color from the button style for SVG fill processing
5911
+ const textColor = style.color || '#000000';
5912
+ // Process SVG to replace currentColor with actual text color
5913
+ const processedSvg = iconSvg
5914
+ ? iconSvg
5915
+ .replace(/fill="currentColor"/gi, `fill="${textColor}"`)
5916
+ .replace(/fill='currentColor'/gi, `fill='${textColor}'`)
5917
+ .replace(/stroke="currentColor"/gi, `stroke="${textColor}"`)
5918
+ .replace(/stroke='currentColor'/gi, `stroke='${textColor}'`)
5919
+ : null;
5920
+ // Render the appropriate icon
5921
+ const renderIcon = () => {
5922
+ // Inline SVG
5923
+ if (processedSvg) {
5924
+ return (_$1("span", { style: Styles.ProviderButtonIcon, "aria-hidden": "true", dangerouslySetInnerHTML: { __html: processedSvg } }));
5925
+ }
5926
+ // Image URL
5927
+ if (iconUrl) {
5928
+ return (_$1("img", { src: iconUrl, alt: "", style: Styles.ProviderButtonIcon, "aria-hidden": "true" }));
5929
+ }
5930
+ return null;
5931
+ };
5932
+ return (_$1("button", { type: "button", style: style, onClick: onClick, class: `dxc-option-btn${styleHint ? ` dxc-option-${styleHint}` : ''}`, "aria-label": displayName },
5933
+ renderIcon(),
5934
+ _$1("span", { style: Styles.ProviderButtonText }, displayName)));
5935
+ }
5936
+ /**
5937
+ * Visual divider with "or" text.
5938
+ */
5939
+ function Divider() {
5940
+ return (_$1("div", { style: Styles.Divider },
5941
+ _$1("div", { style: Styles.DividerLine }),
5942
+ _$1("span", { style: Styles.DividerText }, "or"),
5943
+ _$1("div", { style: Styles.DividerLine })));
5944
+ }
5945
+
5421
5946
  const OTP_LENGTH = 8;
5422
- function LoginDialog({ title, type, alerts, fields, submitLabel, cancelLabel, onCancel, onSubmit, }) {
5947
+ /**
5948
+ * Generic dialog that can render:
5949
+ * - Form fields (text inputs)
5950
+ * - Selectable options (buttons)
5951
+ * - Or both together
5952
+ *
5953
+ * When an option is clicked, calls onSubmit({ [option.name]: option.value }).
5954
+ * This unified approach means the same callback handles both form submission
5955
+ * and option selection.
5956
+ */
5957
+ function LoginDialog({ title, alerts, fields, options, submitLabel, cancelLabel, onCancel, onSubmit, }) {
5423
5958
  const [params, setParams] = d({});
5424
5959
  const firstFieldRef = A(null);
5425
5960
  _(() => { var _a; return (_a = firstFieldRef.current) === null || _a === void 0 ? void 0 : _a.focus(); }, []);
5961
+ const fieldEntries = Object.entries(fields || {});
5962
+ const hasFields = fieldEntries.length > 0;
5963
+ const hasOptions = options && options.length > 0;
5964
+ // Group options by name to detect if we have multiple groups
5965
+ const optionGroups = new Map();
5966
+ if (options) {
5967
+ for (const option of options) {
5968
+ const group = optionGroups.get(option.name) || [];
5969
+ group.push(option);
5970
+ optionGroups.set(option.name, group);
5971
+ }
5972
+ }
5973
+ const hasMultipleGroups = optionGroups.size > 1;
5974
+ // Handler for option clicks - calls onSubmit with { [option.name]: option.value }
5975
+ const handleOptionClick = (option) => {
5976
+ onSubmit({ [option.name]: option.value });
5977
+ };
5426
5978
  return (_$1(Dialog, { className: "dxc-login-dlg" },
5427
5979
  _$1(k$1, null,
5428
5980
  _$1("h3", { style: Styles.WindowHeader }, title),
5429
- alerts.map((alert) => (_$1("p", { style: Styles.Alert[alert.type] }, resolveText(alert)))),
5430
- _$1("form", { onSubmit: (ev) => {
5981
+ alerts.map((alert, idx) => (_$1("p", { key: idx, style: Styles.Alert[alert.type] }, resolveText(alert)))),
5982
+ hasOptions && (_$1("div", { class: "dxc-options" }, hasMultipleGroups ? (
5983
+ // Render with dividers between groups
5984
+ Array.from(optionGroups.entries()).map(([groupName, groupOptions], groupIdx) => (_$1(k$1, { key: groupName },
5985
+ groupIdx > 0 && _$1(Divider, null),
5986
+ groupOptions.map((option) => (_$1(OptionButton, { key: `${option.name}-${option.value}`, option: option, onClick: () => handleOptionClick(option) }))))))) : (
5987
+ // Simple case: all options in one group
5988
+ options.map((option) => (_$1(OptionButton, { key: `${option.name}-${option.value}`, option: option, onClick: () => handleOptionClick(option) })))))),
5989
+ hasOptions && hasFields && _$1(Divider, null),
5990
+ hasFields && (_$1("form", { onSubmit: (ev) => {
5431
5991
  ev.preventDefault();
5432
5992
  onSubmit(params);
5433
- } }, Object.entries(fields).map(([fieldName, { type, label, placeholder }], idx) => (_$1("label", { style: Styles.Label, key: idx },
5993
+ } }, fieldEntries.map(([fieldName, { type, label, placeholder }], idx) => (_$1("label", { style: Styles.Label, key: idx },
5434
5994
  label ? `${label}: ` : '',
5435
5995
  _$1("input", { ref: idx === 0 ? firstFieldRef : undefined, type: type, name: fieldName, autoComplete: "on", style: Styles.Input, autoFocus: true, placeholder: placeholder, value: params[fieldName] || '', onInput: (ev) => {
5436
5996
  var _a;
@@ -5441,10 +6001,10 @@ function LoginDialog({ title, type, alerts, fields, submitLabel, cancelLabel, on
5441
6001
  // Auto-submit when OTP is filled in.
5442
6002
  onSubmit(updatedParams);
5443
6003
  }
5444
- } })))))),
6004
+ } }))))))),
5445
6005
  _$1("div", { style: Styles.ButtonsDiv },
5446
6006
  _$1(k$1, null,
5447
- _$1("button", { type: "submit", style: Styles.PrimaryButton, onClick: () => onSubmit(params) }, submitLabel),
6007
+ hasFields && submitLabel && (_$1("button", { type: "submit", style: Styles.PrimaryButton, onClick: () => onSubmit(params) }, submitLabel)),
5448
6008
  cancelLabel && (_$1("button", { style: Styles.Button, onClick: onCancel }, cancelLabel))))));
5449
6009
  }
5450
6010
  function valueTransformer(type, value) {
@@ -5476,7 +6036,8 @@ class LoginGui extends x {
5476
6036
  render(props, { userInteraction }) {
5477
6037
  if (!userInteraction)
5478
6038
  return null;
5479
- //if (props.db.cloud.userInteraction.observers.length > 1) return null; // Someone else subscribes.
6039
+ // LoginDialog handles all interaction types uniformly
6040
+ // (forms with fields, options, or both)
5480
6041
  return _$1(LoginDialog, Object.assign({}, userInteraction));
5481
6042
  }
5482
6043
  }
@@ -6035,6 +6596,83 @@ function createAwareness(db, doc, provider) {
6035
6596
  return awareness;
6036
6597
  }
6037
6598
 
6599
+ /**
6600
+ * Decodes a base64url-encoded string to a regular string.
6601
+ * Base64url uses - instead of + and _ instead of /, and may omit padding.
6602
+ */
6603
+ function decodeBase64Url(encoded) {
6604
+ // Add padding if needed
6605
+ const padded = encoded + '='.repeat((4 - (encoded.length % 4)) % 4);
6606
+ // Convert base64url to base64
6607
+ const base64 = padded.replace(/-/g, '+').replace(/_/g, '/');
6608
+ return atob(base64);
6609
+ }
6610
+ /**
6611
+ * Parses OAuth callback parameters from the dxc-auth query parameter.
6612
+ *
6613
+ * The dxc-auth parameter contains base64url-encoded JSON with the following structure:
6614
+ * - On success: { "code": "...", "provider": "...", "state": "..." }
6615
+ * - On error: { "error": "...", "provider": "...", "state": "..." }
6616
+ *
6617
+ * @param url - The URL to parse (defaults to window.location.href)
6618
+ * @returns OAuthCallbackParams if valid callback, null otherwise
6619
+ * @throws OAuthError if there's an error in the callback
6620
+ */
6621
+ function parseOAuthCallback(url) {
6622
+ const targetUrl = (typeof window !== 'undefined' ? window.location.href : '');
6623
+ if (!targetUrl) {
6624
+ return null;
6625
+ }
6626
+ const parsed = new URL(targetUrl);
6627
+ const encoded = parsed.searchParams.get('dxc-auth');
6628
+ if (!encoded) {
6629
+ return null; // Not an OAuth callback URL
6630
+ }
6631
+ let payload;
6632
+ try {
6633
+ const json = decodeBase64Url(encoded);
6634
+ payload = JSON.parse(json);
6635
+ }
6636
+ catch (e) {
6637
+ console.warn('[dexie-cloud] Failed to parse dxc-auth parameter:', e);
6638
+ return null;
6639
+ }
6640
+ const { code, provider, state, error } = payload;
6641
+ // Check for error first
6642
+ if (error) {
6643
+ if (error.toLowerCase().includes('access_denied') || error.toLowerCase().includes('access denied')) {
6644
+ throw new OAuthError('access_denied', provider, error);
6645
+ }
6646
+ if (error.toLowerCase().includes('email') && error.toLowerCase().includes('verif')) {
6647
+ throw new OAuthError('email_not_verified', provider, error);
6648
+ }
6649
+ throw new OAuthError('provider_error', provider, error);
6650
+ }
6651
+ // Validate required fields for success case
6652
+ if (!code || !provider || !state) {
6653
+ console.warn('[dexie-cloud] Invalid dxc-auth payload: missing required fields');
6654
+ return null;
6655
+ }
6656
+ return { code, provider, state };
6657
+ }
6658
+ /**
6659
+ * Cleans up the dxc-auth query parameter from the URL.
6660
+ * Call this after successfully handling the callback to clean up the browser URL.
6661
+ */
6662
+ function cleanupOAuthUrl() {
6663
+ var _a;
6664
+ if (typeof window === 'undefined' || !((_a = window.history) === null || _a === void 0 ? void 0 : _a.replaceState)) {
6665
+ return;
6666
+ }
6667
+ const url = new URL(window.location.href);
6668
+ if (!url.searchParams.has('dxc-auth')) {
6669
+ return;
6670
+ }
6671
+ url.searchParams.delete('dxc-auth');
6672
+ const cleanUrl = url.pathname + (url.searchParams.toString() ? `?${url.searchParams.toString()}` : '') + url.hash;
6673
+ window.history.replaceState(null, '', cleanUrl);
6674
+ }
6675
+
6038
6676
  function getTiedRealmId(objectId) {
6039
6677
  return 'rlm~' + objectId;
6040
6678
  }
@@ -6217,6 +6855,8 @@ function dexieCloud(dexie) {
6217
6855
  const currentUserEmitter = getCurrentUserEmitter(dexie);
6218
6856
  const subscriptions = [];
6219
6857
  let configuredProgramatically = false;
6858
+ // Pending OAuth auth code from dxc-auth redirect (detected in configure())
6859
+ let pendingOAuthCode = null;
6220
6860
  // local sync worker - used when there's no service worker.
6221
6861
  let localSyncWorker = null;
6222
6862
  dexie.on('ready', (dexie) => __awaiter(this, void 0, void 0, function* () {
@@ -6246,7 +6886,7 @@ function dexieCloud(dexie) {
6246
6886
  const syncComplete = new Subject();
6247
6887
  dexie.cloud = {
6248
6888
  // @ts-ignore
6249
- version: "4.2.5",
6889
+ version: "4.3.2",
6250
6890
  options: Object.assign({}, DEFAULT_OPTIONS),
6251
6891
  schema: null,
6252
6892
  get currentUserId() {
@@ -6281,6 +6921,26 @@ function dexieCloud(dexie) {
6281
6921
  DexieCloudDB(dexie).reconfigure(); // Update observable from new dexie.name
6282
6922
  }
6283
6923
  updateSchemaFromOptions(dexie.cloud.schema, dexie.cloud.options);
6924
+ // Check for OAuth callback (dxc-auth query parameter)
6925
+ // Only check in DOM environment, not workers
6926
+ if (typeof window !== 'undefined' && window.location) {
6927
+ try {
6928
+ const callback = parseOAuthCallback();
6929
+ if (callback) {
6930
+ // Clean up URL immediately (remove dxc-auth param)
6931
+ cleanupOAuthUrl();
6932
+ // Store the pending auth code for processing when db is ready
6933
+ pendingOAuthCode = { code: callback.code, provider: callback.provider };
6934
+ console.debug('[dexie-cloud] OAuth callback detected, auth code stored for processing');
6935
+ }
6936
+ }
6937
+ catch (error) {
6938
+ // parseOAuthCallback throws OAuthError on error callbacks
6939
+ // Store null for code but log the error
6940
+ console.warn('[dexie-cloud] OAuth callback error:', error);
6941
+ cleanupOAuthUrl();
6942
+ }
6943
+ }
6284
6944
  },
6285
6945
  logout() {
6286
6946
  return __awaiter(this, arguments, void 0, function* ({ force } = {}) {
@@ -6475,6 +7135,19 @@ function dexieCloud(dexie) {
6475
7135
  // HERE: If requireAuth, do athentication now.
6476
7136
  let changedUser = false;
6477
7137
  const user = yield db.getCurrentUser();
7138
+ // Process pending OAuth callback if present (from dxc-auth redirect)
7139
+ if (pendingOAuthCode && !db.cloud.isServiceWorkerDB) {
7140
+ const { code, provider } = pendingOAuthCode;
7141
+ pendingOAuthCode = null; // Clear pending code
7142
+ console.debug('[dexie-cloud] Processing OAuth callback, provider:', provider);
7143
+ try {
7144
+ changedUser = yield login(db, { oauthCode: code, provider });
7145
+ }
7146
+ catch (error) {
7147
+ console.error('[dexie-cloud] OAuth login failed:', error);
7148
+ // Continue with normal flow - user can try again
7149
+ }
7150
+ }
6478
7151
  const requireAuth = (_b = db.cloud.options) === null || _b === void 0 ? void 0 : _b.requireAuth;
6479
7152
  if (requireAuth) {
6480
7153
  if (db.cloud.isServiceWorkerDB) {
@@ -6563,7 +7236,7 @@ function dexieCloud(dexie) {
6563
7236
  }
6564
7237
  }
6565
7238
  // @ts-ignore
6566
- dexieCloud.version = "4.2.5";
7239
+ dexieCloud.version = "4.3.2";
6567
7240
  Dexie.Cloud = dexieCloud;
6568
7241
 
6569
7242
  export { dexieCloud as default, defineYDocTrigger, dexieCloud, getTiedObjectId, getTiedRealmId, resolveText };