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など)にリセットコードが送信されます。

func resetPassword(username: String) async {
do {
let resetResult = try await Amplify.Auth.resetPassword(for: username)
switch resetResult.nextStep {
case .confirmResetPasswordWithCode(let deliveryDetails, let info):
print("Confirm reset password with code send to - \(deliveryDetails) \(String(describing: info))")
case .done:
print("Reset completed")
}
} catch let error as AuthError {
print("Reset password failed with error \(error)")
} catch {
print("Unexpected error: \(error)")
}
}
func resetPassword(username: String) -> AnyCancellable {
Amplify.Publisher.create {
try await Amplify.Auth.resetPassword(for: username)
}.sink {
if case let .failure(authError) = $0 {
print("Reset password failed with error \(authError)")
}
}
receiveValue: { resetResult in
switch resetResult.nextStep {
case .confirmResetPasswordWithCode(let deliveryDetails, let info):
print("Confirm reset password with code send to - \(deliveryDetails) \(String(describing: info))")
case .done:
print("Reset completed")
}
}
}

通常、パスワードをリセットするにはパスワードのリセットを試みたのが実際のユーザーであることを確認する必要があります。上記の次のステップは.confirmResetPasswordWithCodeになります。

エラーが発生した場合に、より具体的なビューまたはメッセージをユーザーに表示したい場合は、underlyingErrorAWSCognitoAuthErrorにダウンキャストして処理できます。

if let authError = error as? AuthError,
let cognitoAuthError = authError.underlyingError as? AWSCognitoAuthError {
switch cognitoAuthError {
case .userNotFound:
print("User not found")
case .invalidParameter:
print("Invalid Parameter)
default:
break
}
}

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

func confirmResetPassword(
username: String,
newPassword: String,
confirmationCode: String
) async {
do {
try await Amplify.Auth.confirmResetPassword(
for: username,
with: newPassword,
confirmationCode: confirmationCode
)
print("Password reset confirmed")
} catch let error as AuthError {
print("Reset password failed with error \(error)")
} catch {
print("Unexpected error: \(error)")
}
}
func confirmResetPassword(
username: String,
newPassword: String,
confirmationCode: String
) -> AnyCancellable {
Amplify.Publisher.create {
try await Amplify.Auth.confirmResetPassword(
for: username,
with: newPassword,
confirmationCode: confirmationCode
)
}.sink {
if case let .failure(authError) = $0 {
print("Reset password failed with error \(authError)")
}
}
receiveValue: {
print("Password reset confirmed")
}
}

パスワードを更新する

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

func changePassword(oldPassword: String, newPassword: String) async {
do {
try await Amplify.Auth.update(oldPassword: oldPassword, to: newPassword)
print("Change password succeeded")
} catch let error as AuthError {
print("Change password failed with error \(error)")
} catch {
print("Unexpected error: \(error)")
}
}
func changePassword(oldPassword: String, newPassword: String) -> AnyCancellable {
Amplify.Publisher.create {
try await Amplify.Auth.update(oldPassword: oldPassword, to: newPassword)
}.sink {
if case let .failure(authError) = $0 {
print("Change password failed with error \(authError)")
}
}
receiveValue: {
print("Change password succeeded")
}
}

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

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

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,
},
};