I have a React app with Amplify that correctly sends SMS for user validation and authentication using the AWS Cognito pool.
The Sign In goes smoothly and the user gets the SMS.
The trouble arises when I try to implement a "Send new SMS" functionality for users that take too long to enter the OTP, and the session expires on them (3 min.).
The following is my normal sign in function:
async componentDidMount() {
const username = "yyy";
const password = "zzz";
user = await Auth.signIn(username, password);
const { challengeName } = user;
console.log(user);
if (!challengeName) {
const { history } = this.props;
history.push("/", { err: "No current user" });
}
}
And this is my "Send new SMS" function:
resendConfirmationCode = async (e) => {
try {
await Auth.resendSignUp(this.state.username);
console.log("code resent successfully");
this.resetTimer();
} catch (err) {
console.log("error resending code: ", err);
}
};
This does indeed send another SMS, but I will get the same error message with the first one that with the new one:
"NotAuthorizedException", message: "Invalid session for the user, session is expired."
I have also tried to refresh the session, and I get a new SMS, but it will result in the same error message:
testingSessionTwo = async (e) => {
try {
const cognitoUser = await Auth.currentAuthenticatedUser();
const currentSession = cognitoUser.signInUserSession;
cognitoUser.refreshSession(currentSession.refreshToken, (err, session) => {
Auth.signOut();
const username = "sss";
const password = "xxx";
Auth.signIn(username, password);
console.log(session);
});
} catch (e) {
// whatever
}
};
What am I missing here?