diff --git a/package.json b/package.json index 4c62c54166..31ef4b10b2 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "scripts": { "build": "yarn workspace @imtbl/sdk updateDependencies && NODE_OPTIONS=--max-old-space-size=14366 nx run-many --target=build --projects=@imtbl/sdk,@imtbl/checkout-widgets && yarn syncpack:format && yarn wsrun -p @imtbl/sdk -a -m copyBrowserBundles", "build:onlysdk": "NODE_OPTIONS=--max-old-space-size=14366 nx run-many --target=build --projects=@imtbl/sdk && yarn syncpack:format", + "dev": "./dev.sh", "docs:build": "typedoc", "docs:serve": "http-server ./docs --cors -p 8080 -c-1", "lint": "wsrun --exclude-missing -e lint --no-error-on-unmatched-pattern", @@ -64,8 +65,7 @@ "test:checkout:sdk:coverage": "wsrun -p @imtbl/checkout-sdk --exclude-missing -e test:coverage", "test:examples": "cd examples/ts-immutable-sample && yarn test && yarn test:e2e", "test:vpn": "RUN_VPN_TESTS=1 wsrun --exclude-missing -e test", - "typecheck": "wsrun --exclude-missing typecheck", - "dev": "./dev.sh" + "typecheck": "wsrun --exclude-missing typecheck" }, "workspaces": { "packages": [ diff --git a/packages/passport/sdk/package.json b/packages/passport/sdk/package.json index ed2308e84c..0afac1498c 100644 --- a/packages/passport/sdk/package.json +++ b/packages/passport/sdk/package.json @@ -21,6 +21,7 @@ "ethers": "^5.7.2", "events": "^3.3.0", "jwt-decode": "^3.1.2", + "localforage": "^1.10.0", "magic-sdk": "^21.2.0", "oidc-client-ts": "2.4.0", "uuid": "^8.3.2" diff --git a/packages/passport/sdk/src/authManager.ts b/packages/passport/sdk/src/authManager.ts index 0f94447b39..f7a26483bb 100644 --- a/packages/passport/sdk/src/authManager.ts +++ b/packages/passport/sdk/src/authManager.ts @@ -11,6 +11,7 @@ import axios from 'axios'; import * as crypto from 'crypto'; import jwt_decode from 'jwt-decode'; import { getDetail, Detail } from '@imtbl/metrics'; +import localForage from 'localforage'; import DeviceCredentialsManager from './storage/device_credentials_manager'; import logger from './utils/logger'; import { isTokenExpired } from './utils/token'; @@ -31,6 +32,7 @@ import { } from './types'; import { PassportConfiguration } from './config'; import Overlay from './overlay'; +import { LocalForageAsyncStorage } from './storage/LocalForageAsyncStorage'; const formUrlEncodedHeader = { headers: { @@ -44,7 +46,14 @@ const authorizeEndpoint = '/authorize'; const getAuthConfiguration = (config: PassportConfiguration): UserManagerSettings => { const { authenticationDomain, oidcConfiguration } = config; - const store = typeof window !== 'undefined' ? window.localStorage : new InMemoryWebStorage(); + let store; + if (config.crossSdkBridgeEnabled) { + store = new LocalForageAsyncStorage('ImmutableSDKPassport', localForage.INDEXEDDB); + } else if (typeof window !== 'undefined') { + store = window.localStorage; + } else { + store = new InMemoryWebStorage(); + } const userStore = new WebStorageStateStore({ store }); const endSessionEndpoint = new URL(logoutEndpoint, authenticationDomain.replace(/^(?:https?:\/\/)?(.*)/, 'https://$1')); diff --git a/packages/passport/sdk/src/storage/LocalForageAsyncStorage.ts b/packages/passport/sdk/src/storage/LocalForageAsyncStorage.ts new file mode 100644 index 0000000000..b0cd008051 --- /dev/null +++ b/packages/passport/sdk/src/storage/LocalForageAsyncStorage.ts @@ -0,0 +1,34 @@ +import { AsyncStorage } from 'oidc-client-ts'; +import localForage from 'localforage'; + +export class LocalForageAsyncStorage implements AsyncStorage { + private storage: LocalForage; + + constructor(name: string, driver: string | string[]) { + this.storage = localForage.createInstance({ name, driver }); + } + + get length(): Promise { + return this.storage.length(); + } + + clear(): Promise { + return this.storage.clear(); + } + + getItem(key: string): Promise { + return this.storage.getItem(key); + } + + key(index: number): Promise { + return this.storage.key(index); + } + + async removeItem(key: string): Promise { + await this.storage.removeItem(key); + } + + async setItem(key: string, value: string): Promise { + await this.storage.setItem(key, value); + } +} diff --git a/sdk/package.json b/sdk/package.json index eb26b85c1f..7126e7a17d 100644 --- a/sdk/package.json +++ b/sdk/package.json @@ -44,6 +44,7 @@ "i18next": "^23.7.6", "i18next-browser-languagedetector": "^7.2.0", "jwt-decode": "^3.1.2", + "localforage": "^1.10.0", "lru-memorise": "0.3.0", "magic-sdk": "^21.2.0", "merkletreejs": "^0.3.11", diff --git a/yarn.lock b/yarn.lock index f0896b9fd0..9ba1692860 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2250,7 +2250,6 @@ __metadata: "@biom3/design-tokens": ~0.4.0 buffer: ^6.0.3 csstype: ^3.1.2 - localforage: ^1.10.0 lodash.debounce: ^4.0.8 lodash.get: ^4.4.2 lodash.isequal: ^4.5.0 @@ -3833,6 +3832,7 @@ __metadata: jest-environment-jsdom: ^29.4.3 jwt-decode: ^3.1.2 jwt-encode: ^1.0.1 + localforage: ^1.10.0 magic-sdk: ^21.2.0 msw: ^1.2.2 oidc-client-ts: 2.4.0 @@ -3923,6 +3923,7 @@ __metadata: i18next: ^23.7.6 i18next-browser-languagedetector: ^7.2.0 jwt-decode: ^3.1.2 + localforage: ^1.10.0 lru-memorise: 0.3.0 magic-sdk: ^21.2.0 merkletreejs: ^0.3.11