@aws-amplify/auth
Version:
Auth category of aws-amplify
53 lines (50 loc) • 1.88 kB
JavaScript
import { decodeJWT, AmplifyError } from '@aws-amplify/core/internals/utils';
import { tokenOrchestrator } from './tokenProvider.mjs';
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
async function cacheCognitoTokens(AuthenticationResult) {
if (AuthenticationResult.AccessToken) {
const accessToken = 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 = 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 tokenOrchestrator.setTokens({
tokens,
});
}
else {
// This would be a service error
throw new AmplifyError({
message: 'Invalid tokens',
name: 'InvalidTokens',
recoverySuggestion: 'Check Cognito UserPool settings',
});
}
}
export { cacheCognitoTokens };
//# sourceMappingURL=cacheTokens.mjs.map