@terreno/syncdb 57.20.0 → 58.0.0-beta.1
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.
- package/CHANGELOG.md +93 -0
- package/README.md +7 -7
- package/dist/api/apiError.d.ts +43 -0
- package/dist/api/apiError.d.ts.map +1 -0
- package/dist/api/apiError.js +28 -0
- package/dist/api/apiError.js.map +1 -0
- package/dist/api/apiFetch.d.ts +44 -0
- package/dist/api/apiFetch.d.ts.map +1 -0
- package/dist/api/apiFetch.js +203 -0
- package/dist/api/apiFetch.js.map +1 -0
- package/dist/api/client.d.ts +70 -0
- package/dist/api/client.d.ts.map +1 -0
- package/dist/api/client.js +240 -0
- package/dist/api/client.js.map +1 -0
- package/dist/api/client.test.d.ts +2 -0
- package/dist/api/client.test.d.ts.map +1 -0
- package/dist/api/client.test.js +588 -0
- package/dist/api/client.test.js.map +1 -0
- package/dist/app/app.d.ts +13 -0
- package/dist/app/app.d.ts.map +1 -0
- package/dist/app/app.js +13 -0
- package/dist/app/app.js.map +1 -0
- package/dist/app/devConsoleLogger.d.ts +18 -0
- package/dist/app/devConsoleLogger.d.ts.map +1 -0
- package/dist/app/devConsoleLogger.js +147 -0
- package/dist/app/devConsoleLogger.js.map +1 -0
- package/dist/app/devConsoleLogger.test.d.ts +2 -0
- package/dist/app/devConsoleLogger.test.d.ts.map +1 -0
- package/dist/app/devConsoleLogger.test.js +202 -0
- package/dist/app/devConsoleLogger.test.js.map +1 -0
- package/dist/app/mcpTools.d.ts +32 -0
- package/dist/app/mcpTools.d.ts.map +1 -0
- package/dist/app/mcpTools.js +61 -0
- package/dist/app/mcpTools.js.map +1 -0
- package/dist/app/permissions.d.ts +26 -0
- package/dist/app/permissions.d.ts.map +1 -0
- package/dist/app/permissions.js +37 -0
- package/dist/app/permissions.js.map +1 -0
- package/dist/app/permissions.test.d.ts +2 -0
- package/dist/app/permissions.test.d.ts.map +1 -0
- package/dist/app/permissions.test.js +36 -0
- package/dist/app/permissions.test.js.map +1 -0
- package/dist/app/upgradeCheck.d.ts +46 -0
- package/dist/app/upgradeCheck.d.ts.map +1 -0
- package/dist/app/upgradeCheck.js +121 -0
- package/dist/app/upgradeCheck.js.map +1 -0
- package/dist/auth/SessionProvider.d.ts +40 -0
- package/dist/auth/SessionProvider.d.ts.map +1 -0
- package/dist/auth/SessionProvider.js +44 -0
- package/dist/auth/SessionProvider.js.map +1 -0
- package/dist/auth/SessionProvider.test.d.ts +2 -0
- package/dist/auth/SessionProvider.test.d.ts.map +1 -0
- package/dist/auth/SessionProvider.test.js +240 -0
- package/dist/auth/SessionProvider.test.js.map +1 -0
- package/dist/auth/TerrenoAccountSecurityScreen.d.ts +25 -0
- package/dist/auth/TerrenoAccountSecurityScreen.d.ts.map +1 -0
- package/dist/auth/TerrenoAccountSecurityScreen.js +21 -0
- package/dist/auth/TerrenoAccountSecurityScreen.js.map +1 -0
- package/dist/auth/TerrenoLoginScreen.d.ts +62 -0
- package/dist/auth/TerrenoLoginScreen.d.ts.map +1 -0
- package/dist/auth/TerrenoLoginScreen.js +45 -0
- package/dist/auth/TerrenoLoginScreen.js.map +1 -0
- package/dist/auth/auth.d.ts +36 -0
- package/dist/auth/auth.d.ts.map +1 -0
- package/dist/auth/auth.js +35 -0
- package/dist/auth/auth.js.map +1 -0
- package/dist/auth/betterAuthAdapter.d.ts +1 -1
- package/dist/auth/betterAuthClient.d.ts +276 -0
- package/dist/auth/betterAuthClient.d.ts.map +1 -0
- package/dist/auth/betterAuthClient.js +744 -0
- package/dist/auth/betterAuthClient.js.map +1 -0
- package/dist/auth/betterAuthClient.types.test.d.ts +2 -0
- package/dist/auth/betterAuthClient.types.test.d.ts.map +1 -0
- package/dist/auth/betterAuthClient.types.test.js +151 -0
- package/dist/auth/betterAuthClient.types.test.js.map +1 -0
- package/dist/auth/betterAuthClientRegistry.d.ts +51 -0
- package/dist/auth/betterAuthClientRegistry.d.ts.map +1 -0
- package/dist/auth/betterAuthClientRegistry.js +69 -0
- package/dist/auth/betterAuthClientRegistry.js.map +1 -0
- package/dist/auth/betterAuthClientRegistry.test.d.ts +2 -0
- package/dist/auth/betterAuthClientRegistry.test.d.ts.map +1 -0
- package/dist/auth/betterAuthClientRegistry.test.js +91 -0
- package/dist/auth/betterAuthClientRegistry.test.js.map +1 -0
- package/dist/auth/betterAuthTypes.d.ts +172 -0
- package/dist/auth/betterAuthTypes.d.ts.map +1 -0
- package/dist/auth/betterAuthTypes.js +7 -0
- package/dist/auth/betterAuthTypes.js.map +1 -0
- package/dist/auth/jwtAuthAdapter.d.ts +102 -0
- package/dist/auth/jwtAuthAdapter.d.ts.map +1 -0
- package/dist/auth/jwtAuthAdapter.js +282 -0
- package/dist/auth/jwtAuthAdapter.js.map +1 -0
- package/dist/auth/jwtAuthAdapter.test.d.ts +2 -0
- package/dist/auth/jwtAuthAdapter.test.d.ts.map +1 -0
- package/dist/auth/jwtAuthAdapter.test.js +614 -0
- package/dist/auth/jwtAuthAdapter.test.js.map +1 -0
- package/dist/auth/loginErrors.d.ts +75 -0
- package/dist/auth/loginErrors.d.ts.map +1 -0
- package/dist/auth/loginErrors.js +154 -0
- package/dist/auth/loginErrors.js.map +1 -0
- package/dist/auth/loginErrors.test.d.ts +2 -0
- package/dist/auth/loginErrors.test.d.ts.map +1 -0
- package/dist/auth/loginErrors.test.js +70 -0
- package/dist/auth/loginErrors.test.js.map +1 -0
- package/dist/auth/ownEntry.d.ts +8 -0
- package/dist/auth/ownEntry.d.ts.map +1 -0
- package/dist/auth/ownEntry.js +13 -0
- package/dist/auth/ownEntry.js.map +1 -0
- package/dist/auth/ownEntry.test.d.ts +2 -0
- package/dist/auth/ownEntry.test.d.ts.map +1 -0
- package/dist/auth/ownEntry.test.js +18 -0
- package/dist/auth/ownEntry.test.js.map +1 -0
- package/dist/auth/platform.d.ts +3 -0
- package/dist/auth/platform.d.ts.map +1 -0
- package/dist/auth/platform.js +4 -0
- package/dist/auth/platform.js.map +1 -0
- package/dist/auth/rememberedAccounts.d.ts +126 -0
- package/dist/auth/rememberedAccounts.d.ts.map +1 -0
- package/dist/auth/rememberedAccounts.js +399 -0
- package/dist/auth/rememberedAccounts.js.map +1 -0
- package/dist/auth/rememberedAccounts.test.d.ts +2 -0
- package/dist/auth/rememberedAccounts.test.d.ts.map +1 -0
- package/dist/auth/rememberedAccounts.test.js +676 -0
- package/dist/auth/rememberedAccounts.test.js.map +1 -0
- package/dist/auth/session.d.ts +118 -0
- package/dist/auth/session.d.ts.map +1 -0
- package/dist/auth/session.js +370 -0
- package/dist/auth/session.js.map +1 -0
- package/dist/auth/session.test.d.ts +2 -0
- package/dist/auth/session.test.d.ts.map +1 -0
- package/dist/auth/session.test.js +825 -0
- package/dist/auth/session.test.js.map +1 -0
- package/dist/auth/tokenExpiry.d.ts +70 -0
- package/dist/auth/tokenExpiry.d.ts.map +1 -0
- package/dist/auth/tokenExpiry.js +123 -0
- package/dist/auth/tokenExpiry.js.map +1 -0
- package/dist/auth/types.d.ts +1 -1
- package/dist/auth/types.js +1 -1
- package/dist/auth/useAccountSecurity.d.ts +183 -0
- package/dist/auth/useAccountSecurity.d.ts.map +1 -0
- package/dist/auth/useAccountSecurity.js +399 -0
- package/dist/auth/useAccountSecurity.js.map +1 -0
- package/dist/auth/useAccountSecurity.types.test.d.ts +2 -0
- package/dist/auth/useAccountSecurity.types.test.d.ts.map +1 -0
- package/dist/auth/useAccountSecurity.types.test.js +21 -0
- package/dist/auth/useAccountSecurity.types.test.js.map +1 -0
- package/dist/auth/useAuthMethods.d.ts +67 -0
- package/dist/auth/useAuthMethods.d.ts.map +1 -0
- package/dist/auth/useAuthMethods.js +77 -0
- package/dist/auth/useAuthMethods.js.map +1 -0
- package/dist/auth/useAuthMethods.test.d.ts +2 -0
- package/dist/auth/useAuthMethods.test.d.ts.map +1 -0
- package/dist/auth/useAuthMethods.test.js +145 -0
- package/dist/auth/useAuthMethods.test.js.map +1 -0
- package/dist/auth/useJwtAuth.d.ts +31 -0
- package/dist/auth/useJwtAuth.d.ts.map +1 -0
- package/dist/auth/useJwtAuth.js +58 -0
- package/dist/auth/useJwtAuth.js.map +1 -0
- package/dist/auth/useJwtAuth.test.d.ts +2 -0
- package/dist/auth/useJwtAuth.test.d.ts.map +1 -0
- package/dist/auth/useJwtAuth.test.js +165 -0
- package/dist/auth/useJwtAuth.test.js.map +1 -0
- package/dist/auth/useLoginController.d.ts +248 -0
- package/dist/auth/useLoginController.d.ts.map +1 -0
- package/dist/auth/useLoginController.js +671 -0
- package/dist/auth/useLoginController.js.map +1 -0
- package/dist/auth/useLoginController.types.test.d.ts +2 -0
- package/dist/auth/useLoginController.types.test.d.ts.map +1 -0
- package/dist/auth/useLoginController.types.test.js +19 -0
- package/dist/auth/useLoginController.types.test.js.map +1 -0
- package/dist/buildNumber.d.ts +34 -0
- package/dist/buildNumber.d.ts.map +1 -0
- package/dist/buildNumber.js +62 -0
- package/dist/buildNumber.js.map +1 -0
- package/dist/buildNumber.test.d.ts +2 -0
- package/dist/buildNumber.test.d.ts.map +1 -0
- package/dist/buildNumber.test.js +114 -0
- package/dist/buildNumber.test.js.map +1 -0
- package/dist/client.d.ts +10 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +237 -184
- package/dist/client.js.map +1 -1
- package/dist/client.socketEvents.test.d.ts +2 -0
- package/dist/client.socketEvents.test.d.ts.map +1 -0
- package/dist/client.socketEvents.test.js +64 -0
- package/dist/client.socketEvents.test.js.map +1 -0
- package/dist/client.test.js +60 -1
- package/dist/client.test.js.map +1 -1
- package/dist/codegen/cli.js +23 -12
- package/dist/codegen/cli.js.map +1 -1
- package/dist/codegen/cli.test.js +57 -0
- package/dist/codegen/cli.test.js.map +1 -1
- package/dist/codegen/discoverCollections.d.ts.map +1 -1
- package/dist/codegen/discoverCollections.js +4 -0
- package/dist/codegen/discoverCollections.js.map +1 -1
- package/dist/codegen/discoverOperations.d.ts +11 -0
- package/dist/codegen/discoverOperations.d.ts.map +1 -0
- package/dist/codegen/discoverOperations.js +135 -0
- package/dist/codegen/discoverOperations.js.map +1 -0
- package/dist/codegen/emitApi.d.ts +8 -0
- package/dist/codegen/emitApi.d.ts.map +1 -0
- package/dist/codegen/emitApi.js +312 -0
- package/dist/codegen/emitApi.js.map +1 -0
- package/dist/codegen/emitApi.test.d.ts +2 -0
- package/dist/codegen/emitApi.test.d.ts.map +1 -0
- package/dist/codegen/emitApi.test.js +360 -0
- package/dist/codegen/emitApi.test.js.map +1 -0
- package/dist/codegen/emitSdk.js +2 -2
- package/dist/codegen/generate.d.ts +6 -1
- package/dist/codegen/generate.d.ts.map +1 -1
- package/dist/codegen/generate.js +18 -0
- package/dist/codegen/generate.js.map +1 -1
- package/dist/codegen/generate.test.js +53 -1
- package/dist/codegen/generate.test.js.map +1 -1
- package/dist/codegen/hookNames.d.ts +11 -0
- package/dist/codegen/hookNames.d.ts.map +1 -1
- package/dist/codegen/hookNames.js +23 -0
- package/dist/codegen/hookNames.js.map +1 -1
- package/dist/codegen/safeIdentifiers.d.ts +5 -1
- package/dist/codegen/safeIdentifiers.d.ts.map +1 -1
- package/dist/codegen/safeIdentifiers.js +7 -2
- package/dist/codegen/safeIdentifiers.js.map +1 -1
- package/dist/codegen/safeIdentifiers.test.js +9 -0
- package/dist/codegen/safeIdentifiers.test.js.map +1 -1
- package/dist/codegen/types.d.ts +46 -1
- package/dist/codegen/types.d.ts.map +1 -1
- package/dist/config/baseUrl.d.ts +13 -0
- package/dist/config/baseUrl.d.ts.map +1 -0
- package/dist/config/baseUrl.js +49 -0
- package/dist/config/baseUrl.js.map +1 -0
- package/dist/config/baseUrl.test.d.ts +2 -0
- package/dist/config/baseUrl.test.d.ts.map +1 -0
- package/dist/config/baseUrl.test.js +42 -0
- package/dist/config/baseUrl.test.js.map +1 -0
- package/dist/config/resolveBaseUrls.d.ts +53 -0
- package/dist/config/resolveBaseUrls.d.ts.map +1 -0
- package/dist/config/resolveBaseUrls.js +80 -0
- package/dist/config/resolveBaseUrls.js.map +1 -0
- package/dist/config/resolveBaseUrls.test.d.ts +2 -0
- package/dist/config/resolveBaseUrls.test.d.ts.map +1 -0
- package/dist/config/resolveBaseUrls.test.js +265 -0
- package/dist/config/resolveBaseUrls.test.js.map +1 -0
- package/dist/crypto/keyProviders.d.ts +2 -2
- package/dist/crypto/keyProviders.d.ts.map +1 -1
- package/dist/crypto/keyProviders.js +15 -4
- package/dist/crypto/keyProviders.js.map +1 -1
- package/dist/crypto/keyProviders.test.js +19 -0
- package/dist/crypto/keyProviders.test.js.map +1 -1
- package/dist/crypto/types.d.ts +5 -0
- package/dist/crypto/types.d.ts.map +1 -1
- package/dist/flags/flagTestServer.d.ts +19 -0
- package/dist/flags/flagTestServer.d.ts.map +1 -0
- package/dist/flags/flagTestServer.js +58 -0
- package/dist/flags/flagTestServer.js.map +1 -0
- package/dist/flags/useFeatureFlags.d.ts +30 -0
- package/dist/flags/useFeatureFlags.d.ts.map +1 -0
- package/dist/flags/useFeatureFlags.js +83 -0
- package/dist/flags/useFeatureFlags.js.map +1 -0
- package/dist/flags/useFeatureFlags.test.d.ts +2 -0
- package/dist/flags/useFeatureFlags.test.d.ts.map +1 -0
- package/dist/flags/useFeatureFlags.test.js +116 -0
- package/dist/flags/useFeatureFlags.test.js.map +1 -0
- package/dist/flags/useTerrenoFeatureFlags.d.ts +50 -0
- package/dist/flags/useTerrenoFeatureFlags.d.ts.map +1 -0
- package/dist/flags/useTerrenoFeatureFlags.js +125 -0
- package/dist/flags/useTerrenoFeatureFlags.js.map +1 -0
- package/dist/flags/useTerrenoFeatureFlags.test.d.ts +2 -0
- package/dist/flags/useTerrenoFeatureFlags.test.d.ts.map +1 -0
- package/dist/flags/useTerrenoFeatureFlags.test.js +259 -0
- package/dist/flags/useTerrenoFeatureFlags.test.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/isolated/TerrenoAccountSecurityScreen.isolated.d.ts +2 -0
- package/dist/isolated/TerrenoAccountSecurityScreen.isolated.d.ts.map +1 -0
- package/dist/isolated/TerrenoAccountSecurityScreen.isolated.js +94 -0
- package/dist/isolated/TerrenoAccountSecurityScreen.isolated.js.map +1 -0
- package/dist/isolated/TerrenoLoginScreen.isolated.d.ts +2 -0
- package/dist/isolated/TerrenoLoginScreen.isolated.d.ts.map +1 -0
- package/dist/isolated/TerrenoLoginScreen.isolated.js +183 -0
- package/dist/isolated/TerrenoLoginScreen.isolated.js.map +1 -0
- package/dist/isolated/betterAuthClient.isolated.d.ts +2 -0
- package/dist/isolated/betterAuthClient.isolated.d.ts.map +1 -0
- package/dist/isolated/betterAuthClient.isolated.js +1734 -0
- package/dist/isolated/betterAuthClient.isolated.js.map +1 -0
- package/dist/isolated/betterAuthClient.native.isolated.d.ts +2 -0
- package/dist/isolated/betterAuthClient.native.isolated.d.ts.map +1 -0
- package/dist/isolated/betterAuthClient.native.isolated.js +153 -0
- package/dist/isolated/betterAuthClient.native.isolated.js.map +1 -0
- package/dist/isolated/mcpTools.isolated.d.ts +2 -0
- package/dist/isolated/mcpTools.isolated.d.ts.map +1 -0
- package/dist/isolated/mcpTools.isolated.js +197 -0
- package/dist/isolated/mcpTools.isolated.js.map +1 -0
- package/dist/isolated/rememberedAccounts.native.isolated.d.ts +2 -0
- package/dist/isolated/rememberedAccounts.native.isolated.d.ts.map +1 -0
- package/dist/isolated/rememberedAccounts.native.isolated.js +50 -0
- package/dist/isolated/rememberedAccounts.native.isolated.js.map +1 -0
- package/dist/isolated/sessionLogout.isolated.d.ts +2 -0
- package/dist/isolated/sessionLogout.isolated.d.ts.map +1 -0
- package/dist/isolated/sessionLogout.isolated.js +133 -0
- package/dist/isolated/sessionLogout.isolated.js.map +1 -0
- package/dist/isolated/tokenExpiry.isolated.d.ts +2 -0
- package/dist/isolated/tokenExpiry.isolated.d.ts.map +1 -0
- package/dist/isolated/tokenExpiry.isolated.js +232 -0
- package/dist/isolated/tokenExpiry.isolated.js.map +1 -0
- package/dist/isolated/upgradeCheck.isolated.d.ts +2 -0
- package/dist/isolated/upgradeCheck.isolated.d.ts.map +1 -0
- package/dist/isolated/upgradeCheck.isolated.js +241 -0
- package/dist/isolated/upgradeCheck.isolated.js.map +1 -0
- package/dist/isolated/useAccountSecurity.isolated.d.ts +2 -0
- package/dist/isolated/useAccountSecurity.isolated.d.ts.map +1 -0
- package/dist/isolated/useAccountSecurity.isolated.js +851 -0
- package/dist/isolated/useAccountSecurity.isolated.js.map +1 -0
- package/dist/isolated/useAccountSecurity.native.isolated.d.ts +2 -0
- package/dist/isolated/useAccountSecurity.native.isolated.d.ts.map +1 -0
- package/dist/isolated/useAccountSecurity.native.isolated.js +52 -0
- package/dist/isolated/useAccountSecurity.native.isolated.js.map +1 -0
- package/dist/isolated/useLoginController.isolated.d.ts +2 -0
- package/dist/isolated/useLoginController.isolated.d.ts.map +1 -0
- package/dist/isolated/useLoginController.isolated.js +1116 -0
- package/dist/isolated/useLoginController.isolated.js.map +1 -0
- package/dist/persisters/defaultPersisterFactory.native.d.ts.map +1 -1
- package/dist/persisters/defaultPersisterFactory.native.js +2 -0
- package/dist/persisters/defaultPersisterFactory.native.js.map +1 -1
- package/dist/persisters/encryptedIndexedDbPersister.d.ts.map +1 -1
- package/dist/persisters/encryptedIndexedDbPersister.js +3 -0
- package/dist/persisters/encryptedIndexedDbPersister.js.map +1 -1
- package/dist/persisters/types.d.ts +6 -0
- package/dist/persisters/types.d.ts.map +1 -1
- package/dist/query/queryWindows.js +2 -2
- package/dist/query/queryWindows.js.map +1 -1
- package/dist/react/apiHooks.d.ts +87 -0
- package/dist/react/apiHooks.d.ts.map +1 -0
- package/dist/react/apiHooks.js +195 -0
- package/dist/react/apiHooks.js.map +1 -0
- package/dist/react/apiHooks.test.d.ts +2 -0
- package/dist/react/apiHooks.test.d.ts.map +1 -0
- package/dist/react/apiHooks.test.js +366 -0
- package/dist/react/apiHooks.test.js.map +1 -0
- package/dist/react/index.d.ts +2 -0
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +2 -0
- package/dist/react/index.js.map +1 -1
- package/dist/react/provider.d.ts +2 -0
- package/dist/react/provider.d.ts.map +1 -1
- package/dist/react/provider.js +2 -0
- package/dist/react/provider.js.map +1 -1
- package/dist/react/useSocketEvent.d.ts +27 -0
- package/dist/react/useSocketEvent.d.ts.map +1 -0
- package/dist/react/useSocketEvent.js +36 -0
- package/dist/react/useSocketEvent.js.map +1 -0
- package/dist/react/useSocketEvent.test.d.ts +2 -0
- package/dist/react/useSocketEvent.test.d.ts.map +1 -0
- package/dist/react/useSocketEvent.test.js +76 -0
- package/dist/react/useSocketEvent.test.js.map +1 -0
- package/dist/sync/replayCoordinator.js +1 -1
- package/dist/sync/replayCoordinator.js.map +1 -1
- package/dist/sync/socketTransport.d.ts.map +1 -1
- package/dist/sync/socketTransport.js +14 -1
- package/dist/sync/socketTransport.js.map +1 -1
- package/dist/sync/socketTransport.test.js +42 -0
- package/dist/sync/socketTransport.test.js.map +1 -1
- package/dist/sync/transport.d.ts +9 -0
- package/dist/sync/transport.d.ts.map +1 -1
- package/dist/sync/transport.js +13 -0
- package/dist/sync/transport.js.map +1 -1
- package/dist/testing/fakeTransport.d.ts +5 -1
- package/dist/testing/fakeTransport.d.ts.map +1 -1
- package/dist/testing/fakeTransport.js +26 -0
- package/dist/testing/fakeTransport.js.map +1 -1
- package/dist/testing/fakeTransport.test.js +15 -0
- package/dist/testing/fakeTransport.test.js.map +1 -1
- package/package.json +96 -8
- package/src/api/apiError.ts +47 -0
- package/src/api/apiFetch.ts +245 -0
- package/src/api/client.test.ts +711 -0
- package/src/api/client.ts +367 -0
- package/src/app/app.ts +30 -0
- package/src/app/devConsoleLogger.test.ts +246 -0
- package/src/app/devConsoleLogger.ts +173 -0
- package/src/app/mcpTools.ts +101 -0
- package/src/app/permissions.test.ts +44 -0
- package/src/app/permissions.ts +58 -0
- package/src/app/upgradeCheck.ts +177 -0
- package/src/auth/SessionProvider.test.tsx +286 -0
- package/src/auth/SessionProvider.tsx +87 -0
- package/src/auth/TerrenoAccountSecurityScreen.tsx +56 -0
- package/src/auth/TerrenoLoginScreen.tsx +131 -0
- package/src/auth/auth.ts +117 -0
- package/src/auth/betterAuthAdapter.ts +1 -1
- package/src/auth/betterAuthClient.ts +997 -0
- package/src/auth/betterAuthClient.types.test.ts +197 -0
- package/src/auth/betterAuthClientRegistry.test.ts +114 -0
- package/src/auth/betterAuthClientRegistry.ts +99 -0
- package/src/auth/betterAuthTypes.ts +189 -0
- package/src/auth/jwtAuthAdapter.test.ts +754 -0
- package/src/auth/jwtAuthAdapter.ts +420 -0
- package/src/auth/loginErrors.test.ts +97 -0
- package/src/auth/loginErrors.ts +249 -0
- package/src/auth/ownEntry.test.ts +22 -0
- package/src/auth/ownEntry.ts +15 -0
- package/src/auth/platform.ts +4 -0
- package/src/auth/rememberedAccounts.test.ts +844 -0
- package/src/auth/rememberedAccounts.ts +568 -0
- package/src/auth/session.test.ts +976 -0
- package/src/auth/session.ts +509 -0
- package/src/auth/tokenExpiry.ts +185 -0
- package/src/auth/types.ts +1 -1
- package/src/auth/useAccountSecurity.ts +678 -0
- package/src/auth/useAccountSecurity.types.test.ts +28 -0
- package/src/auth/useAuthMethods.test.ts +181 -0
- package/src/auth/useAuthMethods.ts +137 -0
- package/src/auth/useJwtAuth.test.tsx +202 -0
- package/src/auth/useJwtAuth.ts +102 -0
- package/src/auth/useLoginController.ts +1038 -0
- package/src/auth/useLoginController.types.test.ts +26 -0
- package/src/buildNumber.test.ts +139 -0
- package/src/buildNumber.ts +75 -0
- package/src/client.socketEvents.test.ts +77 -0
- package/src/client.test.ts +73 -1
- package/src/client.ts +87 -18
- package/src/codegen/cli.test.ts +66 -0
- package/src/codegen/cli.ts +23 -12
- package/src/codegen/discoverCollections.ts +4 -0
- package/src/codegen/discoverOperations.ts +172 -0
- package/src/codegen/emitApi.test.tsx +445 -0
- package/src/codegen/emitApi.ts +396 -0
- package/src/codegen/emitSdk.ts +2 -2
- package/src/codegen/fixtures/openapi.api.json +840 -0
- package/src/codegen/generate.test.ts +54 -1
- package/src/codegen/generate.ts +20 -1
- package/src/codegen/hookNames.ts +35 -0
- package/src/codegen/safeIdentifiers.test.ts +20 -0
- package/src/codegen/safeIdentifiers.ts +7 -2
- package/src/codegen/types.ts +45 -1
- package/src/config/baseUrl.test.ts +53 -0
- package/src/config/baseUrl.ts +68 -0
- package/src/config/resolveBaseUrls.test.ts +308 -0
- package/src/config/resolveBaseUrls.ts +118 -0
- package/src/crypto/keyProviders.test.ts +23 -0
- package/src/crypto/keyProviders.ts +15 -4
- package/src/crypto/types.ts +5 -0
- package/src/flags/flagTestServer.ts +77 -0
- package/src/flags/useFeatureFlags.test.tsx +146 -0
- package/src/flags/useFeatureFlags.ts +130 -0
- package/src/flags/useTerrenoFeatureFlags.test.tsx +329 -0
- package/src/flags/useTerrenoFeatureFlags.ts +188 -0
- package/src/index.ts +2 -1
- package/src/isolated/TerrenoAccountSecurityScreen.isolated.tsx +123 -0
- package/src/isolated/TerrenoLoginScreen.isolated.tsx +256 -0
- package/src/isolated/betterAuthClient.isolated.ts +2198 -0
- package/src/isolated/betterAuthClient.native.isolated.ts +173 -0
- package/src/isolated/mcpTools.isolated.ts +259 -0
- package/src/isolated/rememberedAccounts.native.isolated.ts +63 -0
- package/src/isolated/sessionLogout.isolated.ts +152 -0
- package/src/isolated/tokenExpiry.isolated.ts +271 -0
- package/src/isolated/upgradeCheck.isolated.tsx +321 -0
- package/src/isolated/useAccountSecurity.isolated.ts +1089 -0
- package/src/isolated/useAccountSecurity.native.isolated.ts +61 -0
- package/src/isolated/useLoginController.isolated.ts +1384 -0
- package/src/persisters/defaultPersisterFactory.native.ts +7 -1
- package/src/persisters/encryptedIndexedDbPersister.ts +3 -0
- package/src/persisters/types.ts +6 -0
- package/src/query/queryWindows.ts +2 -2
- package/src/react/apiHooks.test.tsx +473 -0
- package/src/react/apiHooks.tsx +336 -0
- package/src/react/index.ts +2 -0
- package/src/react/provider.tsx +3 -0
- package/src/react/useSocketEvent.test.tsx +96 -0
- package/src/react/useSocketEvent.ts +59 -0
- package/src/sync/replayCoordinator.ts +1 -1
- package/src/sync/socketTransport.test.ts +49 -0
- package/src/sync/socketTransport.ts +15 -0
- package/src/sync/transport.ts +25 -0
- package/src/testing/fakeTransport.test.ts +18 -0
- package/src/testing/fakeTransport.ts +36 -5
|
@@ -0,0 +1,509 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-agnostic session state for `@terreno/syncdb/auth`.
|
|
3
|
+
*
|
|
4
|
+
* A {@link SessionSource} knows how to read, watch, and end a session (Better Auth today,
|
|
5
|
+
* the ported JWT adapter later). {@link createSessionStore} turns a source into observable
|
|
6
|
+
* session state — `{userId, user, isLoading, isAuthenticated, error}` — plus `refresh` and
|
|
7
|
+
* `logout`. `SessionProvider` is a thin React binding over this store, which replaces the
|
|
8
|
+
* pre-58 rtk Better Auth Redux slice.
|
|
9
|
+
*/
|
|
10
|
+
import {getBetterAuthClientWebAuth} from "./betterAuthClientRegistry";
|
|
11
|
+
import type {BetterAuthSessionClientLike, BetterAuthUser} from "./betterAuthTypes";
|
|
12
|
+
|
|
13
|
+
/** Default cadence for the polling fallback when a source cannot push session changes. */
|
|
14
|
+
export const DEFAULT_SESSION_POLL_INTERVAL_MS = 60_000;
|
|
15
|
+
|
|
16
|
+
/** Error message recorded when an explicit session sync fails. */
|
|
17
|
+
export const SESSION_SYNC_ERROR = "Failed to sync session";
|
|
18
|
+
|
|
19
|
+
/** How long `logout` waits, by default, for the server's sign-out and the session check after. */
|
|
20
|
+
export const DEFAULT_SIGN_OUT_TIMEOUT_MS = 10_000;
|
|
21
|
+
|
|
22
|
+
/** The `error` a `logout` that left the session in place sets. */
|
|
23
|
+
export const SIGN_OUT_FAILED_MESSAGE = "Couldn't sign out. Check your connection and try again.";
|
|
24
|
+
|
|
25
|
+
/** Settles with the promise, or rejects once `ms` pass. */
|
|
26
|
+
const withTimeout = async <T>(promise: Promise<T>, ms: number): Promise<T> => {
|
|
27
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
28
|
+
const timeout = new Promise<never>((_resolve, reject) => {
|
|
29
|
+
timer = setTimeout(() => reject(new Error(`Timed out after ${ms}ms`)), ms);
|
|
30
|
+
});
|
|
31
|
+
try {
|
|
32
|
+
return await Promise.race([promise, timeout]);
|
|
33
|
+
} finally {
|
|
34
|
+
clearTimeout(timer);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** The signed-in identity a source reports. */
|
|
39
|
+
export interface SessionSnapshot<TUser = BetterAuthUser> {
|
|
40
|
+
userId: string;
|
|
41
|
+
user: TUser;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Listener for pushed session changes. A snapshot (or `null` for signed out) is applied
|
|
46
|
+
* directly; `undefined` means "something changed, re-read the session".
|
|
47
|
+
*/
|
|
48
|
+
export type SessionChangeListener<TUser = BetterAuthUser> = (
|
|
49
|
+
snapshot: SessionSnapshot<TUser> | null | undefined
|
|
50
|
+
) => void;
|
|
51
|
+
|
|
52
|
+
/** Where session state comes from. */
|
|
53
|
+
export interface SessionSource<TUser = BetterAuthUser> {
|
|
54
|
+
/** Read the current session; `null` when signed out. Throws when the read itself fails. */
|
|
55
|
+
getSession: () => Promise<SessionSnapshot<TUser> | null>;
|
|
56
|
+
/** Push session changes (login, logout, user switch). Returns an unsubscribe. */
|
|
57
|
+
subscribe?: (listener: SessionChangeListener<TUser>) => () => void;
|
|
58
|
+
/** End the session on the auth server. */
|
|
59
|
+
signOut: () => Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Whether the client may still hold a live session when the server cannot be asked (after a
|
|
62
|
+
* failed or timed-out sign-out). Default `true`: keep the session rather than guess it is gone.
|
|
63
|
+
*/
|
|
64
|
+
mayHoldSession?: () => boolean;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Session state exposed by `useSession()`. */
|
|
68
|
+
export interface SessionState<TUser = BetterAuthUser> {
|
|
69
|
+
userId: string | null;
|
|
70
|
+
user: TUser | null;
|
|
71
|
+
isAuthenticated: boolean;
|
|
72
|
+
isLoading: boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Whether a `logout()` is waiting for the server. The session stays in the state meanwhile, so
|
|
75
|
+
* the app keeps its screen; show this on the sign-out control.
|
|
76
|
+
*/
|
|
77
|
+
isSigningOut: boolean;
|
|
78
|
+
error: string | null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** A local-first client to tear down on logout (`SyncDb.signOut` stops it and wipes local data when `wipeOnSignOut` is set). */
|
|
82
|
+
export interface SessionSyncDb {
|
|
83
|
+
signOut: () => Promise<void>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface SessionStoreConfig<TUser = BetterAuthUser> {
|
|
87
|
+
/** Session source. Use this or `authClient`. */
|
|
88
|
+
source?: SessionSource<TUser>;
|
|
89
|
+
/** Better Auth client, wrapped with {@link betterAuthSessionSource}. Use this or `source`. */
|
|
90
|
+
authClient?: BetterAuthSessionClientLike;
|
|
91
|
+
/**
|
|
92
|
+
* syncdb clients to sign out (stop + wipe when configured) on logout. They sign out before
|
|
93
|
+
* the auth server so `wipeOnSignOut` still knows whose local data to wipe.
|
|
94
|
+
*/
|
|
95
|
+
syncDbs?: readonly SessionSyncDb[];
|
|
96
|
+
/**
|
|
97
|
+
* How long `logout()` waits for the server's sign-out, and for the session check after a
|
|
98
|
+
* failed one, before treating the server as unreachable.
|
|
99
|
+
* @default 10000
|
|
100
|
+
*/
|
|
101
|
+
signOutTimeoutMs?: number;
|
|
102
|
+
/** Extra app cleanup run on logout after the auth and syncdb sign-outs. */
|
|
103
|
+
onLogout?: () => void | Promise<void>;
|
|
104
|
+
/**
|
|
105
|
+
* Polling cadence used only when the source has no `subscribe` (default
|
|
106
|
+
* {@link DEFAULT_SESSION_POLL_INTERVAL_MS}; `0` disables polling).
|
|
107
|
+
*/
|
|
108
|
+
pollIntervalMs?: number;
|
|
109
|
+
/** Interval scheduler, injectable for deterministic tests (default global setInterval). */
|
|
110
|
+
setIntervalFn?: (handler: () => void, ms: number) => unknown;
|
|
111
|
+
/** Interval canceller matching `setIntervalFn` (default global clearInterval). */
|
|
112
|
+
clearIntervalFn?: (handle: unknown) => void;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export interface SessionStore<TUser = BetterAuthUser> {
|
|
116
|
+
/** Current state snapshot (stable identity until the state changes). */
|
|
117
|
+
getState: () => SessionState<TUser>;
|
|
118
|
+
/** Subscribe to state changes. Returns an unsubscribe. */
|
|
119
|
+
subscribe: (listener: () => void) => () => void;
|
|
120
|
+
/** Watch the source and run the startup sync. Idempotent; restartable after `stop()`. */
|
|
121
|
+
start: () => void;
|
|
122
|
+
/** Stop watching the source; in-flight reads are ignored. */
|
|
123
|
+
stop: () => void;
|
|
124
|
+
/** Re-read the session now (e.g. right after a sign-in). Marks the state loading while it runs. */
|
|
125
|
+
refresh: () => Promise<void>;
|
|
126
|
+
/**
|
|
127
|
+
* Sign out of the auth server while the session stays in the state (`isSigningOut` is true),
|
|
128
|
+
* then sign out syncdb clients (wiping when configured), run `onLogout`, and clear the
|
|
129
|
+
* session. When the server refuses or cannot be reached and the session is still live, it
|
|
130
|
+
* keeps the session, sets `error` to {@link SIGN_OUT_FAILED_MESSAGE}, and wipes nothing.
|
|
131
|
+
*/
|
|
132
|
+
logout: () => Promise<void>;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const INITIAL_STATE = {
|
|
136
|
+
error: null,
|
|
137
|
+
isAuthenticated: false,
|
|
138
|
+
isLoading: true,
|
|
139
|
+
isSigningOut: false,
|
|
140
|
+
user: null,
|
|
141
|
+
userId: null,
|
|
142
|
+
} as const;
|
|
143
|
+
|
|
144
|
+
const SIGNED_OUT_STATE = {...INITIAL_STATE, isLoading: false} as const;
|
|
145
|
+
|
|
146
|
+
interface BetterAuthPayload {
|
|
147
|
+
user?: {id?: unknown} | null;
|
|
148
|
+
session?: unknown;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Unwrap Better Auth's `{data}` envelope (or a direct payload) into a session snapshot. */
|
|
152
|
+
const toSnapshot = (payload: unknown): SessionSnapshot | null => {
|
|
153
|
+
if (!payload || typeof payload !== "object") {
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
const data = "data" in payload ? (payload as {data?: unknown}).data : payload;
|
|
157
|
+
if (!data || typeof data !== "object") {
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
const {session, user} = data as BetterAuthPayload;
|
|
161
|
+
if (!session || !user || typeof user.id !== "string" || !user.id) {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
return {user: user as unknown as BetterAuthUser, userId: user.id};
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const errorMessage = (error: unknown): string | null => {
|
|
168
|
+
if (!error) {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
if (typeof error === "string") {
|
|
172
|
+
return error;
|
|
173
|
+
}
|
|
174
|
+
const message = (error as {message?: unknown}).message;
|
|
175
|
+
return typeof message === "string" && message ? message : "Unknown error";
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
type AtomSubscribe = (listener: (value: unknown) => void) => () => void;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Find the Better Auth session atom: `useSession.subscribe` on nanostore-style clients, or
|
|
182
|
+
* `$store.atoms.session` on the React client (whose `useSession` is a hook).
|
|
183
|
+
*/
|
|
184
|
+
const findSessionAtomSubscribe = (client: BetterAuthSessionClientLike): AtomSubscribe | null => {
|
|
185
|
+
const useSessionSubscribe = (client.useSession as {subscribe?: unknown} | undefined)?.subscribe;
|
|
186
|
+
if (typeof useSessionSubscribe === "function") {
|
|
187
|
+
return useSessionSubscribe as AtomSubscribe;
|
|
188
|
+
}
|
|
189
|
+
const atom = (client.$store as {atoms?: {session?: {subscribe?: unknown}}} | undefined)?.atoms
|
|
190
|
+
?.session;
|
|
191
|
+
if (atom && typeof atom.subscribe === "function") {
|
|
192
|
+
return (listener): (() => void) => (atom.subscribe as AtomSubscribe)(listener);
|
|
193
|
+
}
|
|
194
|
+
return null;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Adapt a Better Auth client to a {@link SessionSource}.
|
|
199
|
+
*
|
|
200
|
+
* - `getSession` throws when Better Auth reports an error, so a failed read is never
|
|
201
|
+
* mistaken for a sign-out.
|
|
202
|
+
* - `subscribe` uses the session atom: in-flight (`isPending`, `isRefetching`) and
|
|
203
|
+
* failed-read (`error`) emissions are skipped (a transient network error is not a logout); session envelopes
|
|
204
|
+
* are applied directly; anything else asks the store to re-read.
|
|
205
|
+
* - `signOut` throws when Better Auth reports an error.
|
|
206
|
+
*/
|
|
207
|
+
export const betterAuthSessionSource = (
|
|
208
|
+
authClient: BetterAuthSessionClientLike
|
|
209
|
+
): SessionSource<BetterAuthUser> => {
|
|
210
|
+
const atomSubscribe = findSessionAtomSubscribe(authClient);
|
|
211
|
+
|
|
212
|
+
const getSession = async (): Promise<SessionSnapshot | null> => {
|
|
213
|
+
const result = await authClient.getSession();
|
|
214
|
+
const failure = errorMessage((result as {error?: unknown} | null | undefined)?.error);
|
|
215
|
+
if (failure) {
|
|
216
|
+
throw new Error(failure);
|
|
217
|
+
}
|
|
218
|
+
return toSnapshot(result);
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const signOut = async (): Promise<void> => {
|
|
222
|
+
const result = await authClient.signOut();
|
|
223
|
+
const failure = errorMessage((result as {error?: unknown} | null | undefined)?.error);
|
|
224
|
+
if (failure) {
|
|
225
|
+
throw new Error(failure);
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
// A web bearer client removes its token before the sign-out request leaves, so with no token
|
|
230
|
+
// it holds nothing; a cookie or native client cannot tell from here.
|
|
231
|
+
const mayHoldSession = (): boolean => {
|
|
232
|
+
const {webAuth, bearerToken} = getBetterAuthClientWebAuth(authClient);
|
|
233
|
+
return webAuth !== "bearer" || Boolean(bearerToken);
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
if (!atomSubscribe) {
|
|
237
|
+
return {getSession, mayHoldSession, signOut};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const subscribe = (listener: SessionChangeListener): (() => void) => {
|
|
241
|
+
const unsubscribe = atomSubscribe((value) => {
|
|
242
|
+
if (!value || typeof value !== "object") {
|
|
243
|
+
listener(undefined);
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
const emission = value as {isPending?: unknown; isRefetching?: unknown; error?: unknown};
|
|
247
|
+
// A refetch re-emits the previous `data` with `isRefetching: true` before the server
|
|
248
|
+
// answers. After a sign-out that stale data still names the old user, so applying it
|
|
249
|
+
// would briefly sign them back in (and fire their requests without a session).
|
|
250
|
+
if (emission.isPending === true || emission.isRefetching === true || emission.error) {
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
if ("data" in emission) {
|
|
254
|
+
listener(toSnapshot(emission));
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
listener(undefined);
|
|
258
|
+
});
|
|
259
|
+
return typeof unsubscribe === "function" ? unsubscribe : (): void => {};
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
return {getSession, mayHoldSession, signOut, subscribe};
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
/** Identity used to skip no-op updates (Better Auth re-emits on every get-session fetch). */
|
|
266
|
+
const snapshotKey = (snapshot: SessionSnapshot<unknown> | null): string => {
|
|
267
|
+
if (!snapshot) {
|
|
268
|
+
return "";
|
|
269
|
+
}
|
|
270
|
+
return `${snapshot.userId}|${JSON.stringify(snapshot.user)}`;
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Create observable session state over a {@link SessionSource}.
|
|
275
|
+
*
|
|
276
|
+
* Session sync happens on `start()` (startup), whenever the source pushes a change (Better
|
|
277
|
+
* Auth's session signal fires after sign-in, sign-out, and session updates), on `refresh()`
|
|
278
|
+
* (call it after a sign-in to resolve immediately), and on `logout()`. Sources without a
|
|
279
|
+
* push channel are polled.
|
|
280
|
+
*/
|
|
281
|
+
export const createSessionStore = <TUser = BetterAuthUser>(
|
|
282
|
+
config: SessionStoreConfig<TUser>
|
|
283
|
+
): SessionStore<TUser> => {
|
|
284
|
+
const source =
|
|
285
|
+
config.source ??
|
|
286
|
+
(config.authClient
|
|
287
|
+
? (betterAuthSessionSource(config.authClient) as unknown as SessionSource<TUser>)
|
|
288
|
+
: undefined);
|
|
289
|
+
if (!source) {
|
|
290
|
+
throw new Error("createSessionStore requires a `source` or an `authClient`");
|
|
291
|
+
}
|
|
292
|
+
const pollIntervalMs = config.pollIntervalMs ?? DEFAULT_SESSION_POLL_INTERVAL_MS;
|
|
293
|
+
const setIntervalFn =
|
|
294
|
+
config.setIntervalFn ??
|
|
295
|
+
((handler: () => void, ms: number): unknown => setInterval(handler, ms));
|
|
296
|
+
const clearIntervalFn =
|
|
297
|
+
config.clearIntervalFn ??
|
|
298
|
+
((handle: unknown): void => clearInterval(handle as ReturnType<typeof setInterval>));
|
|
299
|
+
|
|
300
|
+
let state: SessionState<TUser> = {...INITIAL_STATE};
|
|
301
|
+
let currentKey: string | undefined;
|
|
302
|
+
const listeners = new Set<() => void>();
|
|
303
|
+
let isStarted = false;
|
|
304
|
+
let unsubscribeSource: (() => void) | undefined;
|
|
305
|
+
let pollHandle: unknown;
|
|
306
|
+
// Bumped by every read and by stop()/logout() so stale reads never overwrite newer state.
|
|
307
|
+
let readGeneration = 0;
|
|
308
|
+
// The running logout, if any. Reads and pushed snapshots wait for it: the session must stay put
|
|
309
|
+
// until the server answers, or a login screen that opens at once finds the live session.
|
|
310
|
+
let logoutInFlight: Promise<void> | null = null;
|
|
311
|
+
// The newest snapshot the source pushed while a logout ran; a different user in it wins after.
|
|
312
|
+
let heldSnapshot: SessionSnapshot<TUser> | null | undefined;
|
|
313
|
+
const signOutTimeoutMs = config.signOutTimeoutMs ?? DEFAULT_SIGN_OUT_TIMEOUT_MS;
|
|
314
|
+
|
|
315
|
+
const setState = (next: SessionState<TUser>): void => {
|
|
316
|
+
state = next;
|
|
317
|
+
for (const listener of listeners) {
|
|
318
|
+
listener();
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
const applySnapshot = (snapshot: SessionSnapshot<TUser> | null): void => {
|
|
323
|
+
const key = snapshotKey(snapshot);
|
|
324
|
+
if (key === currentKey && !state.isLoading && state.error === null) {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
currentKey = key;
|
|
328
|
+
if (!snapshot) {
|
|
329
|
+
setState({...SIGNED_OUT_STATE});
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
setState({
|
|
333
|
+
error: null,
|
|
334
|
+
isAuthenticated: true,
|
|
335
|
+
isLoading: false,
|
|
336
|
+
isSigningOut: false,
|
|
337
|
+
user: snapshot.user,
|
|
338
|
+
userId: snapshot.userId,
|
|
339
|
+
});
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Read the session. A loud read (startup, refresh) marks the state loading and, on
|
|
344
|
+
* failure, records the error and clears the session like the rtk slice did. A silent read
|
|
345
|
+
* (polling, opaque push) keeps the current session when the read fails.
|
|
346
|
+
*/
|
|
347
|
+
const readSession = async ({silent}: {silent: boolean}): Promise<void> => {
|
|
348
|
+
if (logoutInFlight !== null) {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
readGeneration += 1;
|
|
352
|
+
const generation = readGeneration;
|
|
353
|
+
if (!silent && !state.isLoading) {
|
|
354
|
+
setState({...state, isLoading: true});
|
|
355
|
+
}
|
|
356
|
+
try {
|
|
357
|
+
const snapshot = await source.getSession();
|
|
358
|
+
if (generation !== readGeneration) {
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
applySnapshot(snapshot);
|
|
362
|
+
} catch (error) {
|
|
363
|
+
if (generation !== readGeneration) {
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
if (silent) {
|
|
367
|
+
console.warn("[terreno/syncdb] Session read failed; keeping the current session", error);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
console.error("[terreno/syncdb] Error syncing session:", error);
|
|
371
|
+
currentKey = undefined;
|
|
372
|
+
setState({...SIGNED_OUT_STATE, error: SESSION_SYNC_ERROR});
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
const onSourceChange = (snapshot: SessionSnapshot<TUser> | null | undefined): void => {
|
|
377
|
+
if (!isStarted) {
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
if (logoutInFlight !== null) {
|
|
381
|
+
if (snapshot !== undefined) {
|
|
382
|
+
heldSnapshot = snapshot;
|
|
383
|
+
}
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
if (snapshot === undefined) {
|
|
387
|
+
void readSession({silent: true});
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
// A pushed snapshot is newer than any read still in flight.
|
|
391
|
+
readGeneration += 1;
|
|
392
|
+
applySnapshot(snapshot);
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
const start = (): void => {
|
|
396
|
+
if (isStarted) {
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
isStarted = true;
|
|
400
|
+
if (source.subscribe) {
|
|
401
|
+
unsubscribeSource = source.subscribe(onSourceChange);
|
|
402
|
+
} else if (pollIntervalMs > 0) {
|
|
403
|
+
pollHandle = setIntervalFn(() => {
|
|
404
|
+
void readSession({silent: true});
|
|
405
|
+
}, pollIntervalMs);
|
|
406
|
+
}
|
|
407
|
+
void readSession({silent: false});
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
const stop = (): void => {
|
|
411
|
+
if (!isStarted) {
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
isStarted = false;
|
|
415
|
+
readGeneration += 1;
|
|
416
|
+
unsubscribeSource?.();
|
|
417
|
+
unsubscribeSource = undefined;
|
|
418
|
+
if (pollHandle !== undefined) {
|
|
419
|
+
clearIntervalFn(pollHandle);
|
|
420
|
+
pollHandle = undefined;
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
const refresh = (): Promise<void> => readSession({silent: false});
|
|
425
|
+
|
|
426
|
+
// After a sign-out that failed or timed out: is the session still live? `true` when the server
|
|
427
|
+
// says so, or when it cannot be asked and the client may still hold one.
|
|
428
|
+
const isSessionStillLive = async (): Promise<boolean> => {
|
|
429
|
+
try {
|
|
430
|
+
const snapshot = await withTimeout(source.getSession(), signOutTimeoutMs);
|
|
431
|
+
return snapshot !== null;
|
|
432
|
+
} catch (error) {
|
|
433
|
+
console.warn("[terreno/syncdb] Could not check the session after a failed sign out:", error);
|
|
434
|
+
return source.mayHoldSession?.() ?? true;
|
|
435
|
+
}
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
// True once the server's session is gone.
|
|
439
|
+
const signOutOfServer = async (): Promise<boolean> => {
|
|
440
|
+
try {
|
|
441
|
+
await withTimeout(source.signOut(), signOutTimeoutMs);
|
|
442
|
+
console.debug("[terreno/syncdb] Signed out");
|
|
443
|
+
return true;
|
|
444
|
+
} catch (error) {
|
|
445
|
+
console.warn("[terreno/syncdb] Error signing out:", error);
|
|
446
|
+
}
|
|
447
|
+
return !(await isSessionStillLive());
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
const runLogout = async (): Promise<void> => {
|
|
451
|
+
const startedUserId = state.userId;
|
|
452
|
+
heldSnapshot = undefined;
|
|
453
|
+
readGeneration += 1;
|
|
454
|
+
setState({...state, error: null, isSigningOut: true});
|
|
455
|
+
|
|
456
|
+
if (!(await signOutOfServer())) {
|
|
457
|
+
// The session is live: keep it and the local data, and say so.
|
|
458
|
+
setState({...state, error: SIGN_OUT_FAILED_MESSAGE, isSigningOut: false});
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// The session stays in the state until here, so apps keep syncdb running and each client's
|
|
463
|
+
// `wipeOnSignOut` still knows whose local data to wipe.
|
|
464
|
+
for (const syncDb of config.syncDbs ?? []) {
|
|
465
|
+
try {
|
|
466
|
+
await syncDb.signOut();
|
|
467
|
+
} catch (error) {
|
|
468
|
+
console.error("[terreno/syncdb] Error signing out sync client:", error);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
try {
|
|
472
|
+
await config.onLogout?.();
|
|
473
|
+
} catch (error) {
|
|
474
|
+
console.error("[terreno/syncdb] onLogout failed:", error);
|
|
475
|
+
}
|
|
476
|
+
readGeneration += 1;
|
|
477
|
+
currentKey = "";
|
|
478
|
+
const held = heldSnapshot as SessionSnapshot<TUser> | null | undefined;
|
|
479
|
+
heldSnapshot = undefined;
|
|
480
|
+
if (held && held.userId !== startedUserId) {
|
|
481
|
+
// Another user signed in while this sign-out ran; it settles only the session it started on.
|
|
482
|
+
applySnapshot(held);
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
setState({...SIGNED_OUT_STATE});
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
const logout = (): Promise<void> => {
|
|
489
|
+
if (logoutInFlight !== null) {
|
|
490
|
+
return logoutInFlight;
|
|
491
|
+
}
|
|
492
|
+
// Deferred a microtask so `logoutInFlight` is set before the source can push a change.
|
|
493
|
+
logoutInFlight = Promise.resolve()
|
|
494
|
+
.then(runLogout)
|
|
495
|
+
.finally((): void => {
|
|
496
|
+
logoutInFlight = null;
|
|
497
|
+
});
|
|
498
|
+
return logoutInFlight;
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
const subscribe = (listener: () => void): (() => void) => {
|
|
502
|
+
listeners.add(listener);
|
|
503
|
+
return (): void => {
|
|
504
|
+
listeners.delete(listener);
|
|
505
|
+
};
|
|
506
|
+
};
|
|
507
|
+
|
|
508
|
+
return {getState: () => state, logout, refresh, start, stop, subscribe};
|
|
509
|
+
};
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token storage and expiry helpers for legacy `@terreno/api` JWT auth (`/auth/login`,
|
|
3
|
+
* `/auth/refresh_token`). Ported from the pre-58 rtk package's `authSlice` / `emptyApi`:
|
|
4
|
+
* tokens live in `expo-secure-store` on native and AsyncStorage on web, under the same
|
|
5
|
+
* `AUTH_TOKEN` / `REFRESH_TOKEN` keys, so a signed-in rtk app stays signed in after the move.
|
|
6
|
+
*/
|
|
7
|
+
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
8
|
+
import * as SecureStore from "expo-secure-store";
|
|
9
|
+
import {jwtDecode} from "jwt-decode";
|
|
10
|
+
import {DateTime} from "luxon";
|
|
11
|
+
|
|
12
|
+
/** Storage key for the short-lived access token. */
|
|
13
|
+
export const AUTH_TOKEN_KEY = "AUTH_TOKEN";
|
|
14
|
+
/** Storage key for the long-lived refresh token. */
|
|
15
|
+
export const REFRESH_TOKEN_KEY = "REFRESH_TOKEN";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Seconds before refresh-token expiry at which the "are you still there?" modal shows. One
|
|
19
|
+
* minute plus five seconds so a user who sees it with a minute left has time to respond.
|
|
20
|
+
*/
|
|
21
|
+
export const STILL_THERE_THRESHOLD_SECS = 65;
|
|
22
|
+
|
|
23
|
+
/** Async key/value storage for JWTs. */
|
|
24
|
+
export interface JwtTokenStorage {
|
|
25
|
+
getItem: (key: string) => Promise<string | null>;
|
|
26
|
+
setItem: (key: string, value: string) => Promise<void>;
|
|
27
|
+
removeItem: (key: string) => Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The stored token pair; either may be missing. */
|
|
31
|
+
export interface JwtTokens {
|
|
32
|
+
token: string | null;
|
|
33
|
+
refreshToken: string | null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Seconds until each token expires (negative once expired); undefined without both tokens. */
|
|
37
|
+
export interface TokenExpirationTimes {
|
|
38
|
+
authRemainingSecs?: number;
|
|
39
|
+
refreshRemainingSecs?: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Claims `@terreno/api`'s `generateTokens` puts in every access and refresh token. */
|
|
43
|
+
export interface JwtClaims {
|
|
44
|
+
exp?: number;
|
|
45
|
+
id?: string;
|
|
46
|
+
sid?: string;
|
|
47
|
+
te?: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* True in a browser (react-native-web), false on iOS/Android. Evaluated per call so syncdb
|
|
52
|
+
* never imports react-native and tests can flip it with a `document` stub.
|
|
53
|
+
*/
|
|
54
|
+
const detectIsWeb = (): boolean => typeof document !== "undefined";
|
|
55
|
+
|
|
56
|
+
const hasWindow = (): boolean => typeof window !== "undefined";
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Token storage that works on web and native: `expo-secure-store` on iOS/Android, and
|
|
60
|
+
* AsyncStorage on web (no secure storage in the browser, so XSS can read it — the same
|
|
61
|
+
* trade-off rtk made). Server-side rendering (no `window`) reads nothing and writes nothing.
|
|
62
|
+
*/
|
|
63
|
+
export const createJwtTokenStorage = ({
|
|
64
|
+
isWeb = detectIsWeb(),
|
|
65
|
+
}: {
|
|
66
|
+
isWeb?: boolean;
|
|
67
|
+
} = {}): JwtTokenStorage => {
|
|
68
|
+
if (!isWeb) {
|
|
69
|
+
return {
|
|
70
|
+
getItem: (key: string): Promise<string | null> => SecureStore.getItemAsync(key),
|
|
71
|
+
removeItem: (key: string): Promise<void> => SecureStore.deleteItemAsync(key),
|
|
72
|
+
setItem: (key: string, value: string): Promise<void> => SecureStore.setItemAsync(key, value),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
getItem: async (key: string): Promise<string | null> => {
|
|
77
|
+
if (!hasWindow()) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
return AsyncStorage.getItem(key);
|
|
81
|
+
},
|
|
82
|
+
removeItem: async (key: string): Promise<void> => {
|
|
83
|
+
if (!hasWindow()) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
await AsyncStorage.removeItem(key);
|
|
87
|
+
},
|
|
88
|
+
setItem: async (key: string, value: string): Promise<void> => {
|
|
89
|
+
if (!hasWindow()) {
|
|
90
|
+
console.warn("[terreno/syncdb] Cannot store auth token: window is not defined (SSR)");
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
await AsyncStorage.setItem(key, value);
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
let defaultStorage: JwtTokenStorage | undefined;
|
|
99
|
+
|
|
100
|
+
/** The platform storage shared by `jwtAuthAdapter` and the expiry helpers by default. */
|
|
101
|
+
export const getDefaultJwtTokenStorage = (): JwtTokenStorage => {
|
|
102
|
+
defaultStorage ??= createJwtTokenStorage();
|
|
103
|
+
return defaultStorage;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/** Read the stored token pair. */
|
|
107
|
+
export const readJwtTokens = async (
|
|
108
|
+
storage: JwtTokenStorage = getDefaultJwtTokenStorage()
|
|
109
|
+
): Promise<JwtTokens> => {
|
|
110
|
+
const [token, refreshToken] = await Promise.all([
|
|
111
|
+
storage.getItem(AUTH_TOKEN_KEY),
|
|
112
|
+
storage.getItem(REFRESH_TOKEN_KEY),
|
|
113
|
+
]);
|
|
114
|
+
return {refreshToken: refreshToken || null, token: token || null};
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/** Decode a JWT's claims without verifying it; `null` when it is not a JWT. */
|
|
118
|
+
export const decodeJwtClaims = (token: string | null | undefined): JwtClaims | null => {
|
|
119
|
+
if (!token) {
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
try {
|
|
123
|
+
return jwtDecode<JwtClaims>(token);
|
|
124
|
+
} catch {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const remainingSecs = (token: string, now: DateTime): number | undefined => {
|
|
130
|
+
const exp = decodeJwtClaims(token)?.exp;
|
|
131
|
+
if (typeof exp !== "number") {
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
return Math.floor(DateTime.fromSeconds(exp).diff(now, "seconds").seconds);
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Seconds until a token pair expires. Both values are undefined unless both tokens are
|
|
139
|
+
* present (rtk behaviour), and a token without a readable `exp` claim reports undefined.
|
|
140
|
+
*/
|
|
141
|
+
export const tokenExpirationTimesFor = ({
|
|
142
|
+
now = DateTime.now(),
|
|
143
|
+
tokens,
|
|
144
|
+
}: {
|
|
145
|
+
now?: DateTime;
|
|
146
|
+
tokens: JwtTokens;
|
|
147
|
+
}): TokenExpirationTimes => {
|
|
148
|
+
if (!tokens.token || !tokens.refreshToken) {
|
|
149
|
+
return {authRemainingSecs: undefined, refreshRemainingSecs: undefined};
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
authRemainingSecs: remainingSecs(tokens.token, now),
|
|
153
|
+
refreshRemainingSecs: remainingSecs(tokens.refreshToken, now),
|
|
154
|
+
};
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
/** Seconds until the stored access and refresh tokens expire. */
|
|
158
|
+
export const getTokenExpirationTimes = async ({
|
|
159
|
+
now,
|
|
160
|
+
storage,
|
|
161
|
+
}: {
|
|
162
|
+
now?: DateTime;
|
|
163
|
+
storage?: JwtTokenStorage;
|
|
164
|
+
} = {}): Promise<TokenExpirationTimes> => {
|
|
165
|
+
return tokenExpirationTimesFor({now, tokens: await readJwtTokens(storage)});
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* True when the refresh token expires within {@link STILL_THERE_THRESHOLD_SECS}: show an
|
|
170
|
+
* "are you still there?" prompt whose confirm action makes any authenticated request (the
|
|
171
|
+
* adapter refreshes the session on it). False when there is no refresh token.
|
|
172
|
+
*/
|
|
173
|
+
export const shouldShowStillThereModal = async ({
|
|
174
|
+
now,
|
|
175
|
+
storage,
|
|
176
|
+
}: {
|
|
177
|
+
now?: DateTime;
|
|
178
|
+
storage?: JwtTokenStorage;
|
|
179
|
+
} = {}): Promise<boolean> => {
|
|
180
|
+
const {refreshRemainingSecs} = await getTokenExpirationTimes({now, storage});
|
|
181
|
+
if (refreshRemainingSecs === undefined) {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
return refreshRemainingSecs <= STILL_THERE_THRESHOLD_SECS;
|
|
185
|
+
};
|
package/src/auth/types.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Structural types for the Better Auth client surface consumed by
|
|
3
3
|
* {@link ../auth/betterAuthAdapter!betterAuthAdapter}. Better Auth is NOT a
|
|
4
4
|
* dependency of this package — these narrow interfaces describe just the
|
|
5
|
-
* pieces syncdb touches (the shape produced by `
|
|
5
|
+
* pieces syncdb touches (the shape produced by `createBetterAuthClient`
|
|
6
6
|
* and the admin-spa client), so any object matching them works.
|
|
7
7
|
*/
|
|
8
8
|
|