パスワード変更とリカバリーの設定
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); }}まとめ
おめでとうございます!ユーザーパスワード変更とリカバリーの設定ガイドを完了しました。このガイドでは、ユーザーがパスワードを変更できるようにし、登録済みのメールまたは電話番号を使用してアカウントを復旧してパスワードを置き換えるのをサポートする方法を学びました。
次のステップ
パスワード管理を有効にしたので、追加の機能を追加することもできます。以下について詳しく学習することをお勧めします: