ユーザー属性の管理
メールアドレスや電話番号などのユーザー属性は、個々のユーザーを識別するのに役立ちます。ユーザープロファイルに含めるユーザー属性を定義することで、ユーザーデータを大規模に管理しやすくなります。この情報は、ユーザージャーニーをパーソナライズし、コンテンツを調整し、直感的なアカウント管理を提供するなど、さまざまな用途に役立ちます。サインアップ時に事前に情報を収集することも、サインアップ後に顧客がプロファイルを更新できるようにすることもできます。このセクションでは、ユーザー属性の操作方法、設定方法、管理方法について詳しく説明します。
サインアップ時にカスタムユーザー属性を設定する
カスタム属性は、signUp APIのuserAttributesオプションで渡すことができます。
func signUp(username: String, password: String, email: String) async { do { let signUpResult = try await Amplify.Auth.signUp( username: username, password: password, options: .init(userAttributes: [ AuthUserAttribute(.email, value: email), AuthUserAttribute(.custom("my-custom-attribute"), value: <custom attribute value>) ]) ) if case let .confirmUser(deliveryDetails, _, userId) = signUpResult.nextStep { print("Delivery details \(String(describing: deliveryDetails)) for userId: \(String(describing: userId)))") } else { print("SignUp Complete") } } catch let error as AuthError { print("An error occurred while registering a user \(error)") } catch { print("Unexpected error: \(error)") }}ユーザー属性を取得する
fetchUserAttributes APIを使用してユーザーのユーザー属性を取得し、プロファイルで読み取ることができます。これはフロントエンドエクスペリエンスをパーソナライズし、ユーザーが見るコンテンツを制御するのに役立ちます。
func fetchAttributes() async { do { let attributes = try await Amplify.Auth.fetchUserAttributes() print("User attributes - \(attributes)") } catch let error as AuthError{ print("Fetching user attributes failed with error \(error)") } catch { print("Unexpected error: \(error)") }}func fetchAttributes() -> AnyCancellable { Amplify.Publisher.create { try await Amplify.Auth.fetchUserAttributes() }.sink { if case let .failure(authError) = $0 { print("Fetch user attributes failed with error \(authError)") } } receiveValue: { attributes in print("User attributes - \(attributes)") }}ユーザー属性を更新する
updateUserAttribute APIを使用して、新しいユーザー属性を作成するか、既存のユーザー属性を更新できます。
func updateAttribute() async { do { let updateResult = try await Amplify.Auth.update( userAttribute: AuthUserAttribute(.phoneNumber, value: "+2223334444") )
switch updateResult.nextStep { case .confirmAttributeWithCode(let deliveryDetails, let info): print("Confirm the attribute with details send to - \(deliveryDetails) \(String(describing: info))") case .done: print("Update completed") } } catch let error as AuthError { print("Update attribute failed with error \(error)") } catch { print("Unexpected error: \(error)") }}func updateAttribute() -> AnyCancellable { Amplify.Publisher.create { try await Amplify.Auth.update( userAttribute: AuthUserAttribute(.phoneNumber, value: "+2223334444") ) }.sink { if case let .failure(authError) = $0 { print("Update attribute failed with error \(authError)") } } receiveValue: { updateResult in switch updateResult.nextStep { case .confirmAttributeWithCode(let deliveryDetails, let info): print("Confirm the attribute with details send to - \(deliveryDetails) \(info)") case .done: print("Update completed") } }}ユーザー属性を確認する
一部の属性は、属性更新を完了するために確認が必要です。属性の確認が必要な場合、updateUserAttributeまたはupdateUserAttributes APIの結果の一部はconfirmAttributeWithCodeになります。確認コードは、配信の詳細で言及された配信媒体に送信されます。ユーザーが確認コードを取得したら、ユーザーがコードを入力するための UIを提示し、ユーザーの入力を使用してconfirmUserAttribute APIを呼び出すことができます。
func confirmAttribute() async { do { try await Amplify.Auth.confirm(userAttribute: .email, confirmationCode: "390739") print("Attribute verified") } catch let error as AuthError { print("Update attribute failed with error \(error)") } catch { print("Unexpected error: \(error)") }}func confirmAttribute() -> AnyCancellable { Amplify.Publisher.create { try await Amplify.Auth.confirm(userAttribute: .email, confirmationCode: "390739") }.sink { if case let .failure(authError) = $0 { print("Update attribute failed with error \(authError)") } } receiveValue: { _ in print("Attribute verified") }}ユーザー属性検証コードを送信する
ユーザーが認証されている間に属性を検証する必要がある場合は、以下に示すようにsendUserAttributeVerificationCode APIを呼び出します。
func sendVerificationCode() async { do { let deliveryDetails = try await Amplify.Auth.sendVerificationCode(forUserAttributeKey: .email) print("Resend code send to - \(deliveryDetails)") } catch let error as AuthError { print("Resend code failed with error \(error)") } catch { print("Unexpected error: \(error)") }}func sendVerificationCode() -> AnyCancellable { Amplify.Publisher.create { try await Amplify.Auth.sendVerificationCode(forUserAttributeKey: .email) }.sink { if case let .failure(authError) = $0 { print("Resend code failed with error \(authError)") } } receiveValue: { deliveryDetails in print("Resend code sent to - \(deliveryDetails)") }}