@aws-amplify/auth
Version:
Auth category of aws-amplify
54 lines (52 loc) • 2 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.cacheCognitoTokens = cacheCognitoTokens;
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
const utils_1 = require("@aws-amplify/core/internals/utils");
const tokenProvider_1 = require("./tokenProvider");
async function cacheCognitoTokens(AuthenticationResult) {
if (AuthenticationResult.AccessToken) {
const accessToken = (0, utils_1.decodeJWT)(AuthenticationResult.AccessToken);
const accessTokenIssuedAtInMillis = (accessToken.payload.iat || 0) * 1000;
const currentTime = new Date().getTime();
const clockDrift = accessTokenIssuedAtInMillis > 0
? accessTokenIssuedAtInMillis - currentTime
: 0;
let idToken;
let refreshToken;
let deviceMetadata;
if (AuthenticationResult.RefreshToken) {
refreshToken = AuthenticationResult.RefreshToken;
}
if (AuthenticationResult.IdToken) {
idToken = (0, utils_1.decodeJWT)(AuthenticationResult.IdToken);
}
if (AuthenticationResult?.NewDeviceMetadata) {
deviceMetadata = AuthenticationResult.NewDeviceMetadata;
}
const tokens = {
accessToken,
idToken,
refreshToken,
clockDrift,
deviceMetadata,
username: AuthenticationResult.username,
};
if (AuthenticationResult?.signInDetails) {
tokens.signInDetails = AuthenticationResult.signInDetails;
}
await tokenProvider_1.tokenOrchestrator.setTokens({
tokens,
});
}
else {
// This would be a service error
throw new utils_1.AmplifyError({
message: 'Invalid tokens',
name: 'InvalidTokens',
recoverySuggestion: 'Check Cognito UserPool settings',
});
}
}
//# sourceMappingURL=cacheTokens.js.map
;