I’m new to AWS Cognito and currently having 2 problems with it:
- in making the login using MFA SMS to work.
- in setting the SMS MFA flag to enabled/disabled
I’m using Node.js and Amazon Cognito Identity SDK for JavaScript [1][2]
My app login flow:
User submits a mobile number (then is redirected to TOTP verification form)
- At this time, the app hits my API endpoint
- and in turn, invokes the function to send TOTP if the number is registered
- User receives the TOTP code via SMS
- User input TOTP code
- The app hits my API endpoint
- and then invokes the function to verify the TOTP
- If TOTP verified, then my API returns Cognito Token to the app
I have managed to send the TOTP via SMS but I can't find a way to verify the TOTP it in order to get the Cognito Token. The sample[3] shown in the Github page uses a prompt, so everything is contained within that function. However, I cannot do that in my case since the 2 calls need to happen on 2 different screens. I've tried separating the sendMFACode() function, but instead of verifying the TOTP, it sends another SMS.
I'm hitting the wall and there is limited docs/resources I've found so far.
Reference:
- https://github.com/aws-amplify/amplify-js/tree/master/packages/amazon-cognito-identity-js
- https://github.com/amazon-archives/amazon-cognito-identity-js/blob/master/src/CognitoUser.js
- https://stackoverflow.com/a/51394500
My user pool settings is 'MFA optional' so I can accommodate login by email method (doesn’t send SMS for login by email. I know that’s odd.) So far, I've managed to set the MFA to required in the code using enableMFA()
function. However, the Github page says that function enableMFA()
is now deprecated, instead, use setUserMfaPreference()
instead. I tried that and got a "SUCCESS" but when I open it in AWS web console, it still states SMS MFA disabled. Is there something I'm missing here?
Here’s my code:
cognitoUser.sendMFACode(VerificationCode, {
onSuccess: (result) => {
console.log(result)
},
onFailure: (error) => {
console.log(error)
}
})
debug result:
{
"code": "InvalidParameterException",
"Name": "InvalidParameterException",
"Message" "Invalid Parameter Required Session"
}
Note: I received the invalid params exception although successfully getting the SMS.