Reputation: 11
"First time posting" so please advise me if I would need to edit the question, I'll try to include to all code related to the problem.
TLDR: I'm using the amazon-cognito-identity-js to implement user authentication in my app. When trying to implement the method to resend the confirmation email I stumble upon this error.
Uncaught TypeError: Cannot set properties of undefined (setting 'username')
const [email, setEmail] = useState();
const [password, setPassword] = useState();
const [username, setUsername] = useState();
const resendConfirmation = () => {
console.log(username);
const userData = {
Username: username,
Pool: UserPool,
};
const cognitoUser = CognitoUser(userData);
cognitoUser.resendConfirmationCode(username, true, (err, data) => {
if (err) {
console.log(err);
alert("Couldn't resend code");
} else {
console.log(data);
alert('code resent');
}
});
};
Anchor tag that calls resendConfirmation()
<form className='login-form' onSubmit={verifyAccount}>
Enter the OTP:
<label className='email-label'>
<div className='div-email'>
<input
type='text'
value={OTP}
onChange={(e) => setOTP(e.target.value)}
/>
</div>
</label>
<br />
<button className='sign-inbtn' type='submit'>
Verify
</button>
<div className='login-footer'>
<p className=''>
Request code again?{' '}
<a className='' onClick={resendConfirmation}>
Resend Code
</a>
</p>
</div>
</form>
const verifyAccount = (e) => {
e.preventDefault();
const user = new CognitoUser({
Username: username,
Pool: UserPool,
});
console.log(user);
user.confirmRegistration(OTP, true, (err, data) => {
if (err) {
console.log(err);
alert("Couldn't verify account");
} else {
console.log(data);
alert('Account verified successfully');
window.location.href = '/login';
}
});
I have checked that my username state does actually contain the username, so I'm not sure why Cognito prompts this error.
Upvotes: 0
Views: 877
Reputation: 1193
Based on the error it seems like the problem is not "username" being undefined but an object you're trying to use which as an attribute called username is undefined.
Upvotes: 1