Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Apr 30, 2024

Maintenance ModeYou are viewing Amplify Gen 1 documentation. Amplify Gen 1 has entered maintenance mode and will reach end of life on May 1, 2027. New project should use Amplify Gen 2. For existing Gen 1 projects, a migration guide and tooling are available to help you upgrade. Switch to the latest Gen 2 docs →

パスワード変更とリカバリーの設定

Amplify Auth は、ユーザーがパスワードを変更したり、忘れたパスワードを復旧したりするための安全な方法を提供します。これはユーザーの摩擦を安全に減らし、アプリケーションへのアクセス体験を向上させます。

開始する前に、以下が必要です:

  • Auth カテゴリが設定されている Amplify プロジェクト
  • インストールして設定された Amplify ライブラリ

パスワードをリセット

ユーザーのパスワードをリセットするには、resetPassword API を使用します。これはユーザーの設定に基づいて、宛先(メールや SMS など)にリセットコードを送信します。

import { resetPassword, type ResetPasswordOutput } from 'aws-amplify/auth';
async function handleResetPassword(username: string) {
try {
const output = await resetPassword({ username });
handleResetPasswordNextSteps(output);
} catch (error) {
console.log(error);
}
}
function handleResetPasswordNextSteps(output: ResetPasswordOutput) {
const { nextStep } = output;
switch (nextStep.resetPasswordStep) {
case 'CONFIRM_RESET_PASSWORD_WITH_CODE':
const codeDeliveryDetails = nextStep.codeDeliveryDetails;
console.log(
`Confirmation code was sent to ${codeDeliveryDetails.deliveryMedium}`
);
// Collect the confirmation code from the user and pass to confirmResetPassword.
break;
case 'DONE':
console.log('Successfully reset password.');
break;
}
}
import { resetPassword } from 'aws-amplify/auth';
async function handleResetPassword(username) {
try {
const output = await resetPassword({ username });
handleResetPasswordNextSteps(output);
} catch (error) {
console.log(error);
}
}
function handleResetPasswordNextSteps(output) {
const { nextStep } = output;
switch (nextStep.resetPasswordStep) {
case 'CONFIRM_RESET_PASSWORD_WITH_CODE':
const codeDeliveryDetails = nextStep.codeDeliveryDetails;
console.log(
`Confirmation code was sent to ${codeDeliveryDetails.deliveryMedium}`
);
// Collect the confirmation code from the user and pass to confirmResetPassword.
break;
case 'DONE':
console.log('Successfully reset password.');
break;
}
}

パスワード リセット プロセスを完了するには、ユーザーが受け取ったコードと設定したい新しいパスワードを使用して confirmResetPassword API を呼び出します。

import {
confirmResetPassword,
type ConfirmResetPasswordInput
} from 'aws-amplify/auth';
async function handleConfirmResetPassword({
username,
confirmationCode,
newPassword
}: ConfirmResetPasswordInput) {
try {
await confirmResetPassword({ username, confirmationCode, newPassword });
} catch (error) {
console.log(error);
}
}
import { confirmResetPassword } from 'aws-amplify/auth';
async function handleConfirmResetPassword({
username,
confirmationCode,
newPassword
}) {
try {
await confirmResetPassword({ username, confirmationCode, newPassword });
} catch (error) {
console.log(error);
}
}

パスワードの更新

updatePassword API を使用して、サインイン中のユーザーのパスワードを更新できます。

import { updatePassword, type UpdatePasswordInput } from 'aws-amplify/auth';
async function handleUpdatePassword({
oldPassword,
newPassword
}: UpdatePasswordInput) {
try {
await updatePassword({ oldPassword, newPassword });
} catch (err) {
console.log(err);
}
}
import { updatePassword } from 'aws-amplify/auth';
async function handleUpdatePassword(oldPassword, newPassword) {
try {
await updatePassword({ oldPassword, newPassword });
} catch (err) {
console.log(err);
}
}

まとめ

おめでとうございます!ユーザーパスワード変更とリカバリーの設定ガイドを完了しました。このガイドでは、ユーザーがパスワードを変更できるようにし、登録済みのメールまたは電話番号を使用してアカウントを復旧してパスワードを置き換えるのをサポートする方法を学びました。

次のステップ

パスワード管理を有効にしたので、追加の機能を追加することもできます。以下について詳しく学習することをお勧めします: