Set up password change and recovery
Amplify Auth provides a secure way for your users to change their password or recover a forgotten password.
If you have not yet created an Amplify (Gen 2) app, visit the quickstart.
Understand password default settings
By default, your users can retrieve access to their accounts if they forgot their password by using either their phone or email. The following are the default account recovery methods used when either phone
or email
are used as login options.
Login option | User account verification channel |
---|---|
phone | Phone Number |
email | |
email and phone |
Override the default user account verification channel
You can always change the channel used by your authentication resources by overriding the following setting.
1// amplify/auth/resource.ts2import { defineAuth } from '@aws-amplify/backend';3
4export const auth = defineAuth({5 loginWith: {6 email: true7 },8+ accountRecovery: 'EMAIL_ONLY'9});
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}
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 ConfirmResetPasswordInput4} from 'aws-amplify/auth';5
6async function handleConfirmResetPassword({7 username,8 confirmationCode,9 newPassword10}: ConfirmResetPasswordInput) {11 try {12 await confirmResetPassword({ username, confirmationCode, newPassword });13 } catch (error) {14 console.log(error);15 }16}
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 newPassword6}: UpdatePasswordInput) {7 try {8 await updatePassword({ oldPassword, newPassword });9 } catch (err) {10 console.log(err);11 }12}
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: