- Newest
- Most votes
- Most comments
In your scenario, you need to regenerate the code when the user clicks the "resend code" button. In order to do this, you might need to make your front-end application initiate a new authentication challenge when this button is clicked. Then, on your back-end side, you should handle this newly initiated challenge by regenerating a new code.
Based on your code, it seems you are already detecting when to generate a new code in the CreateAuthChallenge function.
I noticed that you are using a constant "Dummyval" to detect the "resend" request. However, to make things clearer, I suggest you to use a specific flag that's easier to understand. For example, you could set a variable named resendCode in the client's request when the resend button is clicked, and then check for this variable in your CreateAuthChallenge function.
For instance, on your front-end, you could include the resend flag in your client's request when the "resend code" button is clicked:
Auth.sendCustomChallengeAnswer(cognitoUser, { answer: '', resendCode: true })
And in your CreateAuthChallenge lambda function, you could modify the code as follows:
exports.handler = async (event) => {
log.info("event: ", event);
let verificationCode = "";
const resendCode = event.request.challengeAnswer.resendCode;
// Regenerate the code if it's the first attempt or if the "resend code" button was clicked.
if (event.request.session.length === 2 || resendCode) {
verificationCode = await buildCodeAndSendEmail(event);
} else {
// ... The rest of your code
}
// ... The rest of your code
return event;
};
Please note that the resendCode flag is set on the client-side and passed with the sendCustomChallengeAnswer call. It is used on the server-side in the CreateAuthChallenge function to decide whether to regenerate the code.
This way, you can have the control to regenerate the code only when the "resend code" button is clicked.
Relevant content
- asked 8 months ago
- asked 4 months ago
- asked 2 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 6 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
@Ercan: you will not get the challengeAnswer key on request object in createAuthChallenge lambda, this is the problem i thought about this and also in verifyAuth challenge you will not get the sessions array in request object