@aws-amplify/auth
Version:
Auth category of aws-amplify
1 lines • 4.11 kB
Source Map (JSON)
{"version":3,"file":"fetchDevices.mjs","sources":["../../../../../src/providers/cognito/apis/fetchDevices.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { Amplify, fetchAuthSession } from '@aws-amplify/core';\nimport { AuthAction, assertTokenProviderConfig, } from '@aws-amplify/core/internals/utils';\nimport { listDevices } from '../utils/clients/CognitoIdentityProvider';\nimport { assertAuthTokens } from '../utils/types';\nimport { getRegion } from '../utils/clients/CognitoIdentityProvider/utils';\nimport { getAuthUserAgentValue } from '../../../utils';\n// Cognito Documentation for max device\n// https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ListDevices.html#API_ListDevices_RequestSyntax\nconst MAX_DEVICES = 60;\n/**\n * Fetches devices that have been remembered using {@link rememberDevice}\n * for the currently authenticated user.\n *\n * @returns FetchDevicesOutput\n * @throws {@link ListDevicesException}\n * @throws AuthTokenConfigException - Thrown when the token provider config is invalid.\n */\nexport async function fetchDevices() {\n const authConfig = Amplify.getConfig().Auth?.Cognito;\n assertTokenProviderConfig(authConfig);\n const { tokens } = await fetchAuthSession();\n assertAuthTokens(tokens);\n const response = await listDevices({\n region: getRegion(authConfig.userPoolId),\n userAgentValue: getAuthUserAgentValue(AuthAction.FetchDevices),\n }, {\n AccessToken: tokens.accessToken.toString(),\n Limit: MAX_DEVICES,\n });\n return parseDevicesResponse(response.Devices ?? []);\n}\nconst parseDevicesResponse = async (devices) => {\n return devices.map(({ DeviceKey: id = '', DeviceAttributes = [], DeviceCreateDate, DeviceLastModifiedDate, DeviceLastAuthenticatedDate, }) => {\n const attributes = DeviceAttributes.reduce((attrs, { Name, Value }) => {\n if (Name && Value) {\n attrs[Name] = Value;\n }\n return attrs;\n }, {});\n return {\n id,\n attributes,\n createDate: DeviceCreateDate\n ? new Date(DeviceCreateDate * 1000)\n : undefined,\n lastModifiedDate: DeviceLastModifiedDate\n ? new Date(DeviceLastModifiedDate * 1000)\n : undefined,\n lastAuthenticatedDate: DeviceLastAuthenticatedDate\n ? new Date(DeviceLastAuthenticatedDate * 1000)\n : undefined,\n };\n });\n};\n"],"names":[],"mappings":";;;;;;;AAAA;AACA;AAOA;AACA;AACA,MAAM,WAAW,GAAG,EAAE,CAAC;AACvB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,eAAe,YAAY,GAAG;AACrC,IAAI,MAAM,UAAU,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC;AACzD,IAAI,yBAAyB,CAAC,UAAU,CAAC,CAAC;AAC1C,IAAI,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,gBAAgB,EAAE,CAAC;AAChD,IAAI,gBAAgB,CAAC,MAAM,CAAC,CAAC;AAC7B,IAAI,MAAM,QAAQ,GAAG,MAAM,WAAW,CAAC;AACvC,QAAQ,MAAM,EAAE,SAAS,CAAC,UAAU,CAAC,UAAU,CAAC;AAChD,QAAQ,cAAc,EAAE,qBAAqB,CAAC,UAAU,CAAC,YAAY,CAAC;AACtE,KAAK,EAAE;AACP,QAAQ,WAAW,EAAE,MAAM,CAAC,WAAW,CAAC,QAAQ,EAAE;AAClD,QAAQ,KAAK,EAAE,WAAW;AAC1B,KAAK,CAAC,CAAC;AACP,IAAI,OAAO,oBAAoB,CAAC,QAAQ,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC;AACxD,CAAC;AACD,MAAM,oBAAoB,GAAG,OAAO,OAAO,KAAK;AAChD,IAAI,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,SAAS,EAAE,EAAE,GAAG,EAAE,EAAE,gBAAgB,GAAG,EAAE,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,2BAA2B,GAAG,KAAK;AAClJ,QAAQ,MAAM,UAAU,GAAG,gBAAgB,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,IAAI,EA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