diff --git a/packages/auth/src/providers/cognito/utils/signInHelpers.ts b/packages/auth/src/providers/cognito/utils/signInHelpers.ts index 1b7ed9e9051..27889cc205f 100644 --- a/packages/auth/src/providers/cognito/utils/signInHelpers.ts +++ b/packages/auth/src/providers/cognito/utils/signInHelpers.ts @@ -155,7 +155,9 @@ export async function handleMFASetupChallenge({ }: HandleAuthChallengeRequest): Promise { const { userPoolId, userPoolClientId, userPoolEndpoint } = config; - if (challengeResponse === 'EMAIL') { + const trimmedChallengeResponse = challengeResponse.trim(); + + if (trimmedChallengeResponse === 'EMAIL') { return { ChallengeName: 'MFA_SETUP', Session: session, @@ -166,7 +168,7 @@ export async function handleMFASetupChallenge({ }; } - if (challengeResponse === 'TOTP') { + if (trimmedChallengeResponse === 'TOTP') { return { ChallengeName: 'MFA_SETUP', Session: session, @@ -181,7 +183,7 @@ export async function handleMFASetupChallenge({ USERNAME: username, }; - const isTOTPCode = /^\d+$/.test(challengeResponse.trim()); + const isTOTPCode = /^\d+$/.test(trimmedChallengeResponse); if (isTOTPCode) { const verifySoftwareToken = createVerifySoftwareTokenClient({ @@ -196,7 +198,7 @@ export async function handleMFASetupChallenge({ userAgentValue: getAuthUserAgentValue(AuthAction.ConfirmSignIn), }, { - UserCode: challengeResponse, + UserCode: trimmedChallengeResponse, Session: session, FriendlyDeviceName: deviceName, }, @@ -227,10 +229,10 @@ export async function handleMFASetupChallenge({ ); } - const isEmail = /^\S+@\S+\.\S+$/.test(challengeResponse.trim()); + const isEmail = /^\S+@\S+\.\S+$/.test(trimmedChallengeResponse); if (isEmail) { - challengeResponses.EMAIL = challengeResponse; + challengeResponses.EMAIL = trimmedChallengeResponse; const jsonReq: RespondToAuthChallengeCommandInput = { ChallengeName: 'MFA_SETUP',