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

Page updated Oct 15, 2024

パスワードを管理する

Amplify Authは、ユーザーがパスワードを変更したり、忘れたパスワードを回復したりするための安全な方法を提供します。

パスワードのデフォルト設定を理解する

デフォルトでは、ユーザーは電話またはメールを使用してパスワードを忘れた場合にアカウントへのアクセスを回復できます。次の表は、ログインオプションとしてphoneまたはemailが使用されている場合に使用されるデフォルトアカウント回復方法です。

ログインオプションユーザーアカウント検証チャネル
phone電話番号
emailメール
emailphoneメール

パスワードをリセットする

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

import { resetPassword } from 'aws-amplify/auth';
const output = await resetPassword({
username: "hello@mycompany.com"
});
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 } from 'aws-amplify/auth';
await confirmResetPassword({
username: "hello@mycompany.com",
confirmationCode: "123456",
newPassword: "hunter3",
});

パスワードを更新する

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

import { updatePassword } from 'aws-amplify/auth';
await updatePassword({
oldPassword: "hunter2",
newPassword: "hunter3",
});

デフォルトユーザーアカウント検証チャネルをオーバーライドする

認証リソースで使用されるチャネルを変更するには、常に次の設定をオーバーライドできます。

amplify/auth/resource.ts
import { defineAuth } from '@aws-amplify/backend';
export const auth = defineAuth({
loginWith: {
email: true
},
accountRecovery: 'EMAIL_ONLY'
});

デフォルトパスワードポリシーをオーバーライドする

デフォルトでは、パスワードポリシーは以下に設定されています:

  • MinLength: 8文字
  • requireLowercase: true
  • requireUppercase: true
  • requireNumbers: true
  • requireSymbols: true
  • tempPasswordValidity: 3日

基本となるcfnUserPoolリソースを変更することで、認証リソースで受け入れられるパスワード形式をカスタマイズできます:

amplify/backend.ts
import { defineBackend } from '@aws-amplify/backend';
import { auth } from './auth/resource';
const backend = defineBackend({
auth,
});
// extract L1 CfnUserPool resources
const { cfnUserPool } = backend.auth.resources.cfnResources;
// modify cfnUserPool policies directly
cfnUserPool.policies = {
passwordPolicy: {
minimumLength: 32,
requireLowercase: true,
requireNumbers: true,
requireSymbols: true,
requireUppercase: true,
temporaryPasswordValidityDays: 20,
},
};