UNPKG

@aws-amplify/auth

Version:

Auth category of aws-amplify

91 lines (88 loc) • 4.46 kB
import { assertTokenProviderConfig } from '@aws-amplify/core/internals/utils'; import { AuthValidationErrorCode } from '../../../errors/types/validation.mjs'; import { assertValidationError } from '../../../errors/utils/assertValidationError.mjs'; import { assertServiceError } from '../../../errors/utils/assertServiceError.mjs'; import { handleCustomAuthFlowWithoutSRP, getActiveSignInUsername, getSignInResult, getSignInResultFromError } from '../utils/signInHelpers.mjs'; import { setActiveSignInState, resetActiveSignInState } from '../../../client/utils/store/signInStore.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 { retryOnResourceNotFoundException } from '../utils/retryOnResourceNotFoundException.mjs'; import { getNewDeviceMetadata } from '../utils/getNewDeviceMetadata.mjs'; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 /** * Signs a user in using a custom authentication flow without password * * @param ctx - The AmplifyContext * @param input - The SignInWithCustomAuthInput object * @returns AuthSignInResult * @throws service: {@link InitiateAuthException } - Cognito service errors thrown during the sign-in process. * @throws validation: {@link AuthValidationErrorCode } - Validation errors thrown when either username or password * are not defined. * @throws SignInWithCustomAuthOutput - Thrown when the token provider config is invalid. */ async function signInWithCustomAuth(ctx, input) { const authConfig = ctx.resourcesConfig.Auth?.Cognito; assertTokenProviderConfig(authConfig); const { username, password, options } = input; const signInDetails = { loginId: username, authFlowType: 'CUSTOM_WITHOUT_SRP', }; const metadata = options?.clientMetadata; assertValidationError(!!username, AuthValidationErrorCode.EmptySignInUsername); assertValidationError(!password, AuthValidationErrorCode.CustomAuthSignInPassword); // Resolve the per-context orchestrator ONCE at the entry point so every step // of the flow uses the context's configured orchestrator. const tokenOrchestrator = resolveTokenOrchestrator(ctx); try { const { ChallengeName: retriedChallengeName, ChallengeParameters: retiredChallengeParameters, AuthenticationResult, Session, } = await retryOnResourceNotFoundException(handleCustomAuthFlowWithoutSRP, [username, metadata, authConfig, tokenOrchestrator], username, tokenOrchestrator); const activeUsername = getActiveSignInUsername(username); // sets up local state used during the sign-in process setActiveSignInState({ signInSession: Session, username: activeUsername, challengeName: retriedChallengeName, signInDetails, }); if (AuthenticationResult) { await cacheCognitoTokens({ username: activeUsername, ...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: retriedChallengeName, challengeParameters: retiredChallengeParameters, }); } catch (error) { resetActiveSignInState(); assertServiceError(error); const result = getSignInResultFromError(error.name); if (result) return result; throw error; } } export { signInWithCustomAuth }; //# sourceMappingURL=signInWithCustomAuth.mjs.map