{"version":3,"file":"IdentityIdStore.mjs","sources":["../../../../../src/providers/cognito/credentialsProvider/IdentityIdStore.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { ConsoleLogger, } from '@aws-amplify/core';\nimport { assertIdentityPoolIdConfig } from '@aws-amplify/core/internals/utils';\nimport { getAuthStorageKeys } from '../tokenProvider/TokenStore';\nimport { IdentityIdStorageKeys } from './types';\nconst logger = new ConsoleLogger('DefaultIdentityIdStore');\nexport class DefaultIdentityIdStore {\n    setAuthConfig(authConfigParam) {\n        assertIdentityPoolIdConfig(authConfigParam.Cognito);\n        this.authConfig = authConfigParam;\n        this._authKeys = createKeysForAuthStorage('Cognito', authConfigParam.Cognito.identityPoolId);\n    }\n    constructor(keyValueStorage) {\n        this._authKeys = {};\n        this._hasGuestIdentityId = false;\n        this.keyValueStorage = keyValueStorage;\n    }\n    async loadIdentityId() {\n        assertIdentityPoolIdConfig(this.authConfig?.Cognito);\n        try {\n            if (this._primaryIdentityId) {\n                return {\n                    id: this._primaryIdentityId,\n                    type: 'primary',\n                };\n            }\n            else {\n                const storedIdentityId = await this.keyValueStorage.getItem(this._authKeys.identityId);\n                if (storedIdentityId) {\n                    this._hasGuestIdentityId = true;\n                    return {\n                        id: storedIdentityId,\n                        type: 'guest',\n                    };\n                }\n                return null;\n            }\n        }\n        catch (err) {\n            logger.log('Error getting stored IdentityId.', err);\n            return null;\n        }\n    }\n    async storeIdentityId(identity) {\n        assertIdentityPoolIdConfig(this.authConfig?.Cognito);\n        if (identity.type === 'guest') {\n            this.keyValueStorage.setItem(this._authKeys.identityId, identity.id);\n            // Clear in-memory storage of primary identityId\n            this._primaryIdentityId = undefined;\n            this._hasGuestIdentityId = true;\n        }\n        else {\n            this._primaryIdentityId = identity.id;\n            // Clear locally stored guest id\n            if (this._hasGuestIdentityId) {\n                this.keyValueStorage.removeItem(this._authKeys.identityId);\n                this._hasGuestIdentityId = false;\n            }\n        }\n    }\n    async clearIdentityId() {\n        this._primaryIdentityId = undefined;\n        await this.keyValueStorage.removeItem(this._authKeys.identityId);\n    }\n}\nconst createKeysForAuthStorage = (provider, identifier) => {\n    return getAuthStorageKeys(IdentityIdStorageKeys)(`com.amplify.${provider}`, identifier);\n};\n"],"names":[],"mappings":";;;;;AAAA;AACA;AAKA,MAAM,MAAM,GAAG,IAAI,aAAa,CAAC,wBAAwB,CAAC;AACnD,MAAM,sBAAsB,CAAC;AACpC,IAAI,aAAa,CAAC,eAAe,EAAE;AACnC,QAAQ,0BAA0B,CAAC,eAAe,CAAC,OAAO,CAAC;AAC3D,QAAQ,IAAI,CAAC,UAAU,GAAG,eAAe;AACzC,QAAQ,IAAI,CAAC,SAAS,GAAG,wBAAwB,CAAC,SAAS,EAAE,eAAe,CAAC,OAAO,CAAC,cAAc,CAAC;AACpG,IAAI;AACJ,IAAI,WAAW,CAAC,eAAe,EAAE;AACjC,QAAQ,IAAI,CAAC,SAAS,GAAG,EAAE;AAC3B,QAAQ,IAAI,CAAC,mBAAmB,GAAG,KAAK;AACxC,QAAQ,IAAI,CAAC,eAAe,GAAG,eAAe;AAC9C,IAAI;AACJ,IAAI,MAAM,cAAc,GAAG;AAC3B,QAAQ,0BAA0B,CAAC,IAAI,CAAC,UAAU,EAAE,OAAO,CAAC;AAC5D,QAAQ,IAAI;AACZ,YAAY,IAAI,IAAI,CAAC,kBAAkB,EAAE;AACzC,gBAAgB,OAAO;AACvB,oBAAoB,EAAE,EAAE,IAAI,CAAC,kBAAkB;AAC/C,oBAAoB,IAAI,EAAE,SAAS;AACnC,iBAAiB;AACjB,YAAY;AACZ,iBAAiB;AACjB,gBAAgB,MAAM,gBAAgB,GAAG,MAAM,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC;AACtG,gBAAgB,IAAI,gBAAgB,EAAE;AACtC,oBAAoB,IAAI,CAAC,mBAAmB,GAAG,IAAI;AACnD,oB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