{"version":3,"file":"IdentityIdProvider.mjs","sources":["../../../../../src/providers/cognito/credentialsProvider/IdentityIdProvider.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { getId } from '@aws-amplify/core';\nimport { AuthError } from '../../../errors/AuthError';\nimport { getRegionFromIdentityPoolId } from '../../../foundation/parsers';\nimport { formLoginsMap } from './utils';\n/**\n * Provides a Cognito identityId\n *\n * @param tokens - The AuthTokens received after SignIn\n * @returns string\n * @throws configuration exceptions: `InvalidIdentityPoolIdException`\n *  - Auth errors that may arise from misconfiguration.\n * @throws service exceptions: {@link GetIdException }\n */\nexport async function cognitoIdentityIdProvider({ tokens, authConfig, identityIdStore, }) {\n    identityIdStore.setAuthConfig({ Cognito: authConfig });\n    // will return null only if there is no identityId cached or if there is an error retrieving it\n    const identityId = await identityIdStore.loadIdentityId();\n    if (identityId) {\n        return identityId.id;\n    }\n    const logins = tokens?.idToken\n        ? formLoginsMap(tokens.idToken.toString())\n        : {};\n    const generatedIdentityId = await generateIdentityId(logins, authConfig);\n    // Store generated identityId\n    identityIdStore.storeIdentityId({\n        id: generatedIdentityId,\n        type: tokens ? 'primary' : 'guest',\n    });\n    return generatedIdentityId;\n}\nasync function generateIdentityId(logins, authConfig) {\n    const identityPoolId = authConfig?.identityPoolId;\n    const region = getRegionFromIdentityPoolId(identityPoolId);\n    // IdentityId is absent so get it using IdentityPoolId with Cognito's GetId API\n    const idResult = \n    // for a first-time user, this will return a brand new identity\n    // for a returning user, this will retrieve the previous identity assocaited with the logins\n    (await getId({\n        region,\n    }, {\n        IdentityPoolId: identityPoolId,\n        Logins: logins,\n    })).IdentityId;\n    if (!idResult) {\n        throw new AuthError({\n            name: 'GetIdResponseException',\n            message: 'Received undefined response from getId operation',\n            recoverySuggestion: 'Make sure to pass a valid identityPoolId in the configuration.',\n        });\n    }\n    return idResult;\n}\n"],"names":[],"mappings":";;;;;AAAA;AACA;AAKA;AACA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