UNPKG

@aws-amplify/auth

Version:

Auth category of aws-amplify

92 lines (88 loc) • 4.46 kB
import { resolveCtxArgs, assertTokenProviderConfig } from '@aws-amplify/core/internals/utils'; import '../../../client/utils/store/autoSignInStore.mjs'; import { signInStore, setActiveSignInState, resetActiveSignInState } from '../../../client/utils/store/signInStore.mjs'; import { AuthError } from '../../../errors/AuthError.mjs'; import { handleChallengeName, getSignInResult, getSignInResultFromError } from '../utils/signInHelpers.mjs'; import { assertServiceError } from '../../../errors/utils/assertServiceError.mjs'; import { assertValidationError } from '../../../errors/utils/assertValidationError.mjs'; import { AuthValidationErrorCode } from '../../../errors/types/validation.mjs'; import { AuthErrorCodes } from '../../../common/AuthErrorStrings.mjs'; import { cacheCognitoTokens } from '../tokenProvider/cacheTokens.mjs'; import '../utils/refreshAuthTokens.mjs'; import '@aws-amplify/core'; import '../utils/types.mjs'; import '../tokenProvider/errorHelpers.mjs'; import '../tokenProvider/tokenProvider.mjs'; import { resolveTokenOrchestrator } from '../tokenProvider/contextTokenOrchestrators.mjs'; import { dispatchSignedInHubEvent } from '../utils/dispatchSignedInHubEvent.mjs'; import { getNewDeviceMetadata } from '../utils/getNewDeviceMetadata.mjs'; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 async function confirmSignIn(...args) { const [ctx, input] = resolveCtxArgs(args); const { challengeResponse, options } = input; const { username, challengeName, signInSession, signInDetails } = signInStore.getState(); const authConfig = ctx.resourcesConfig.Auth?.Cognito; assertTokenProviderConfig(authConfig); const clientMetaData = options?.clientMetadata; assertValidationError(!!challengeResponse, 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 = resolveTokenOrchestrator(ctx); if (!username || !challengeName || !signInSession) // TODO: remove this error message for production apps throw new AuthError({ name: 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 handleChallengeName(username, challengeName, signInSession, challengeResponse, authConfig, tokenOrchestrator, clientMetaData, options); // sets up local state used during the sign-in process setActiveSignInState({ signInSession: Session, username, challengeName: handledChallengeName, signInDetails, }); if (AuthenticationResult) { await cacheCognitoTokens({ username, ...AuthenticationResult, NewDeviceMetadata: await getNewDeviceMetadata({ userPoolId: authConfig.userPoolId, userPoolEndpoint: authConfig.userPoolEndpoint, newDeviceMetadata: AuthenticationResult.NewDeviceMetadata, accessToken: AuthenticationResult.AccessToken, }), signInDetails, }, tokenOrchestrator); resetActiveSignInState(); await dispatchSignedInHubEvent(ctx); return { isSignedIn: true, nextStep: { signInStep: 'DONE' }, }; } return getSignInResult(ctx, { challengeName: handledChallengeName, challengeParameters: handledChallengeParameters, }); } catch (error) { assertServiceError(error); const result = getSignInResultFromError(error.name); if (result) return result; throw error; } } export { confirmSignIn }; //# sourceMappingURL=confirmSignIn.mjs.map