Authentication category of AWS Amplify providing APIs and building blocks for creating authentication experiences with Amazon Cognito
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Password reset, confirmation, and update operations for user account security.
Initiate a password reset flow for a user.
function resetPassword(input: ResetPasswordInput): Promise<ResetPasswordOutput>;
interface ResetPasswordInput {
username: string;
options?: {
clientMetadata?: Record<string, string>;
};
}
interface ResetPasswordOutput {
nextStep: {
resetPasswordStep: 'CONFIRM_RESET_PASSWORD_WITH_CODE' | 'DONE';
codeDeliveryDetails?: CodeDeliveryDetails;
};
}import { resetPassword } from "@aws-amplify/auth";
const { nextStep } = await resetPassword({
username: "user@example.com"
});
if (nextStep.resetPasswordStep === 'CONFIRM_RESET_PASSWORD_WITH_CODE') {
console.log(`Reset code sent to: ${nextStep.codeDeliveryDetails?.destination}`);
}Complete the password reset process using the verification code.
function confirmResetPassword(input: ConfirmResetPasswordInput): Promise<void>;
interface ConfirmResetPasswordInput {
username: string;
confirmationCode: string;
newPassword: string;
options?: {
clientMetadata?: Record<string, string>;
};
}import { confirmResetPassword } from "@aws-amplify/auth";
await confirmResetPassword({
username: "user@example.com",
confirmationCode: "123456",
newPassword: "NewSecurePassword123!"
});
console.log("Password reset successfully");Update the password for a signed-in user.
function updatePassword(input: UpdatePasswordInput): Promise<void>;
interface UpdatePasswordInput {
oldPassword: string;
newPassword: string;
}import { updatePassword } from "@aws-amplify/auth";
await updatePassword({
oldPassword: "CurrentPassword123!",
newPassword: "NewSecurePassword456!"
});
console.log("Password updated successfully");When setting passwords, ensure they meet your Cognito User Pool password policy requirements. Common requirements include:
Password operations may throw various errors:
import { resetPassword, AuthError } from "@aws-amplify/auth";
try {
await resetPassword({ username: "user@example.com" });
} catch (error) {
if (error instanceof AuthError) {
switch (error.name) {
case 'UserNotFoundException':
console.log('User not found');
break;
case 'LimitExceededException':
console.log('Too many requests, please try again later');
break;
case 'InvalidParameterException':
console.log('Invalid username format');
break;
default:
console.log('Password reset failed:', error.message);
}
}
}