Page updated Jan 16, 2024

Set up password change and recovery

Amplify Auth provides a secure way for your users to change their password or recover a forgotten password. This securely reduces friction for your users and improves their experience accessing your application.

Before you begin, you will need:

  • An Amplify project with the Auth category configured
  • The Amplify libraries installed and configured

Reset Password

To reset a user's password, use the resetPassword API which will send a reset code to the destination (e.g. email or SMS) based on the user's settings.

1import { resetPassword, type ResetPasswordOutput } from 'aws-amplify/auth';
2
3async function handleResetPassword(username: string) {
4 try {
5 const output = await resetPassword({ username });
6 handleResetPasswordNextSteps(output);
7 } catch (error) {
8 console.log(error);
9 }
10}
11
12function handleResetPasswordNextSteps(output: ResetPasswordOutput) {
13 const { nextStep } = output;
14 switch (nextStep.resetPasswordStep) {
15 case 'CONFIRM_RESET_PASSWORD_WITH_CODE':
16 const codeDeliveryDetails = nextStep.codeDeliveryDetails;
17 console.log(
18 `Confirmation code was sent to ${codeDeliveryDetails.deliveryMedium}`
19 );
20 // Collect the confirmation code from the user and pass to confirmResetPassword.
21 break;
22 case 'DONE':
23 console.log('Successfully reset password.');
24 break;
25 }
26}
1import { resetPassword } from 'aws-amplify/auth';
2
3async function handleResetPassword(username) {
4 try {
5 const output = await resetPassword({ username });
6 handleResetPasswordNextSteps(output);
7 } catch (error) {
8 console.log(error);
9 }
10}
11
12function handleResetPasswordNextSteps(output) {
13 const { nextStep } = output;
14 switch (nextStep.resetPasswordStep) {
15 case 'CONFIRM_RESET_PASSWORD_WITH_CODE':
16 const codeDeliveryDetails = nextStep.codeDeliveryDetails;
17 console.log(
18 `Confirmation code was sent to ${codeDeliveryDetails.deliveryMedium}`
19 );
20 // Collect the confirmation code from the user and pass to confirmResetPassword.
21 break;
22 case 'DONE':
23 console.log('Successfully reset password.');
24 break;
25 }
26}

To complete the password reset process, invoke the confirmResetPassword API with the code your user received and the new password they want to set.

1import {
2 confirmResetPassword,
3 type ConfirmResetPasswordInput
4} from 'aws-amplify/auth';
5
6async function handleConfirmResetPassword({
7 username,
8 confirmationCode,
9 newPassword
10}: ConfirmResetPasswordInput) {
11 try {
12 await confirmResetPassword({ username, confirmationCode, newPassword });
13 } catch (error) {
14 console.log(error);
15 }
16}
1import { confirmResetPassword } from 'aws-amplify/auth';
2
3async function handleConfirmResetPassword({
4 username,
5 confirmationCode,
6 newPassword
7}) {
8 try {
9 await confirmResetPassword({ username, confirmationCode, newPassword });
10 } catch (error) {
11 console.log(error);
12 }
13}

Update password

You can update a signed in user's password using the updatePassword API.

1import { updatePassword, type UpdatePasswordInput } from 'aws-amplify/auth';
2
3async function handleUpdatePassword({
4 oldPassword,
5 newPassword
6}: UpdatePasswordInput) {
7 try {
8 await updatePassword({ oldPassword, newPassword });
9 } catch (err) {
10 console.log(err);
11 }
12}
1import { updatePassword } from 'aws-amplify/auth';
2
3async function handleUpdatePassword(oldPassword, newPassword) {
4 try {
5 await updatePassword({ oldPassword, newPassword });
6 } catch (err) {
7 console.log(err);
8 }
9}

Conclusion

Congratulations! You finished the Set up user password change and recovery guide. In this guide, you learned how to enable password changes by your users and help them recover their account and replace their password using a registered email or phone number.

Next steps

Now that you enabled password management you may also want to add some additional features. We recommend you learn more about: