@aws-amplify/auth
Version:
Auth category of aws-amplify
74 lines (72 loc) • 3.13 kB
JavaScript
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchDevices = fetchDevices;
const core_1 = require("@aws-amplify/core");
const utils_1 = require("@aws-amplify/core/internals/utils");
const types_1 = require("../utils/types");
const parsers_1 = require("../../../foundation/parsers");
const utils_2 = require("../../../utils");
const cognitoIdentityProvider_1 = require("../../../foundation/factories/serviceClients/cognitoIdentityProvider");
const factories_1 = require("../factories");
// Cognito Documentation for max device
// https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ListDevices.html#API_ListDevices_RequestSyntax
const MAX_DEVICES = 60;
/**
* Fetches devices that have been remembered using {@link rememberDevice}
* for the currently authenticated user.
*
* @returns FetchDevicesOutput
* @throws {@link ListDevicesException}
* @throws AuthTokenConfigException - Thrown when the token provider config is invalid.
*/
async function fetchDevices() {
const authConfig = core_1.Amplify.getConfig().Auth?.Cognito;
(0, utils_1.assertTokenProviderConfig)(authConfig);
const { userPoolEndpoint, userPoolId } = authConfig;
const { tokens } = await (0, core_1.fetchAuthSession)();
(0, types_1.assertAuthTokens)(tokens);
const listDevices = (0, cognitoIdentityProvider_1.createListDevicesClient)({
endpointResolver: (0, factories_1.createCognitoUserPoolEndpointResolver)({
endpointOverride: userPoolEndpoint,
}),
});
const response = await listDevices({
region: (0, parsers_1.getRegionFromUserPoolId)(userPoolId),
userAgentValue: (0, utils_2.getAuthUserAgentValue)(utils_1.AuthAction.FetchDevices),
}, {
AccessToken: tokens.accessToken.toString(),
Limit: MAX_DEVICES,
});
return parseDevicesResponse(response.Devices ?? []);
}
const parseDevicesResponse = async (devices) => {
return devices.map(({ DeviceKey: id = '', DeviceAttributes = [], DeviceCreateDate, DeviceLastModifiedDate, DeviceLastAuthenticatedDate, }) => {
let deviceName;
const attributes = DeviceAttributes.reduce((attrs, { Name, Value }) => {
if (Name && Value) {
if (Name === 'device_name')
deviceName = Value;
attrs[Name] = Value;
}
return attrs;
}, {});
const result = {
id,
name: deviceName,
attributes,
createDate: DeviceCreateDate
? new Date(DeviceCreateDate * 1000)
: undefined,
lastModifiedDate: DeviceLastModifiedDate
? new Date(DeviceLastModifiedDate * 1000)
: undefined,
lastAuthenticatedDate: DeviceLastAuthenticatedDate
? new Date(DeviceLastAuthenticatedDate * 1000)
: undefined,
};
return result;
});
};
//# sourceMappingURL=fetchDevices.js.map
;