@aws-amplify/auth
Version:
Auth category of aws-amplify
1 lines • 4.86 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 { assertAuthTokens } from '../utils/types';\nimport { getRegionFromUserPoolId } from '../../../foundation/parsers';\nimport { getAuthUserAgentValue } from '../../../utils';\nimport { createListDevicesClient } from '../../../foundation/factories/serviceClients/cognitoIdentityProvider';\nimport { createCognitoUserPoolEndpointResolver } from '../factories';\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 { userPoolEndpoint, userPoolId } = authConfig;\n const { tokens } = await fetchAuthSession();\n assertAuthTokens(tokens);\n const listDevices = createListDevicesClient({\n endpointResolver: createCognitoUserPoolEndpointResolver({\n endpointOverride: userPoolEndpoint,\n }),\n });\n const response = await listDevices({\n region: getRegionFromUserPoolId(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 let deviceName;\n const attributes = DeviceAttributes.reduce((attrs, { Name, Value }) => {\n if (Name && Value) {\n if (Name === 'device_name')\n deviceName = Value;\n attrs[Name] = Value;\n }\n return attrs;\n }, {});\n const result = {\n id,\n name: deviceName,\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 return result;\n });\n};\n"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA;AACA;AAQA;AACA;AACA,MAAM,WAAW,GAAG,EAAE;AACtB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,eAAe,YAAY,GAAG;AACrC,IAAI,MAAM,UAAU,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC,IAAI,EAAE,OAAO;AACxD,IAAI,yBAAyB,CAAC,UAAU,CAAC;AACzC,IAAI,MAAM,EAAE,gBAAgB,EAAE,UAAU,EAAE,GAAG,UAAU;AACvD,IAAI,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,gBAAgB,EAAE;AAC/C,IAAI,gBAAgB,CAAC,MAAM,CAAC;AAC5B,IAAI,MAAM,WAAW,GAAG,uBAAuB,CAAC;AAChD,QAAQ,gBAAgB,EAAE,qCAAqC,CAAC;AAChE,YAAY,gBAAgB,EAAE,gBAAgB;AAC9C,SAAS,CAAC;AACV,KAAK,CAAC;AACN,IAAI,MAAM,QAAQ,GAAG,MAAM,WAAW,CAAC;AACvC,QAAQ,MAAM,EAAE,uBAAuB,CAAC,UAAU,CAAC;AACnD,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;A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