@aws-amplify/auth
Version:
Auth category of aws-amplify
87 lines (84 loc) • 4.41 kB
JavaScript
'use strict';
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
Object.defineProperty(exports, "__esModule", { value: true });
exports.confirmSignIn = confirmSignIn;
const utils_1 = require("@aws-amplify/core/internals/utils");
const store_1 = require("../../../client/utils/store");
const AuthError_1 = require("../../../errors/AuthError");
const signInHelpers_1 = require("../utils/signInHelpers");
const assertServiceError_1 = require("../../../errors/utils/assertServiceError");
const assertValidationError_1 = require("../../../errors/utils/assertValidationError");
const validation_1 = require("../../../errors/types/validation");
const AuthErrorStrings_1 = require("../../../common/AuthErrorStrings");
const cacheTokens_1 = require("../tokenProvider/cacheTokens");
const tokenProvider_1 = require("../tokenProvider");
const dispatchSignedInHubEvent_1 = require("../utils/dispatchSignedInHubEvent");
const getNewDeviceMetadata_1 = require("../utils/getNewDeviceMetadata");
async function confirmSignIn(...args) {
const [ctx, input] = (0, utils_1.resolveCtxArgs)(args);
const { challengeResponse, options } = input;
const { username, challengeName, signInSession, signInDetails } = store_1.signInStore.getState();
const authConfig = ctx.resourcesConfig.Auth?.Cognito;
(0, utils_1.assertTokenProviderConfig)(authConfig);
const clientMetaData = options?.clientMetadata;
(0, assertValidationError_1.assertValidationError)(!!challengeResponse, validation_1.AuthValidationErrorCode.EmptyChallengeResponse);
// Resolve the per-context orchestrator ONCE at the entry point so every step
// of the challenge flow uses the context's configured orchestrator.
const tokenOrchestrator = (0, tokenProvider_1.resolveTokenOrchestrator)(ctx);
if (!username || !challengeName || !signInSession)
// TODO: remove this error message for production apps
throw new AuthError_1.AuthError({
name: AuthErrorStrings_1.AuthErrorCodes.SignInException,
message: `
An error occurred during the sign in process.
This most likely occurred due to:
1. signIn was not called before confirmSignIn.
2. signIn threw an exception.
3. page was refreshed during the sign in flow and session has expired.
`,
recoverySuggestion: 'Make sure a successful call to signIn is made before calling confirmSignIn' +
'and that the session has not expired.',
});
try {
const { Session, ChallengeName: handledChallengeName, AuthenticationResult, ChallengeParameters: handledChallengeParameters, } = await (0, signInHelpers_1.handleChallengeName)(username, challengeName, signInSession, challengeResponse, authConfig, tokenOrchestrator, clientMetaData, options);
// sets up local state used during the sign-in process
(0, store_1.setActiveSignInState)({
signInSession: Session,
username,
challengeName: handledChallengeName,
signInDetails,
});
if (AuthenticationResult) {
await (0, cacheTokens_1.cacheCognitoTokens)({
username,
...AuthenticationResult,
NewDeviceMetadata: await (0, getNewDeviceMetadata_1.getNewDeviceMetadata)({
userPoolId: authConfig.userPoolId,
userPoolEndpoint: authConfig.userPoolEndpoint,
newDeviceMetadata: AuthenticationResult.NewDeviceMetadata,
accessToken: AuthenticationResult.AccessToken,
}),
signInDetails,
}, tokenOrchestrator);
(0, store_1.resetActiveSignInState)();
await (0, dispatchSignedInHubEvent_1.dispatchSignedInHubEvent)(ctx);
return {
isSignedIn: true,
nextStep: { signInStep: 'DONE' },
};
}
return (0, signInHelpers_1.getSignInResult)(ctx, {
challengeName: handledChallengeName,
challengeParameters: handledChallengeParameters,
});
}
catch (error) {
(0, assertServiceError_1.assertServiceError)(error);
const result = (0, signInHelpers_1.getSignInResultFromError)(error.name);
if (result)
return result;
throw error;
}
}
//# sourceMappingURL=confirmSignIn.js.map