Manage user attributes
These docs cover Amplify Gen 1. For new web apps, we recommend the latest AWS Amplify, with a simplified CLI and native integrations for databases, storage, and AI models. Get started with the latest AWS Amplify.
Fetch the current user's attributes
Invoke the following api to get the list of attributes assigned to the user.
func fetchAttributes() { Amplify.Auth.fetchUserAttributes() { result in switch result { case .success(let attributes): print("User attributes - \(attributes)") case .failure(let error): print("Fetching user attributes failed with error \(error)") } }}func fetchAttributes() -> AnyCancellable { Amplify.Auth.fetchUserAttributes() .resultPublisher .sink { if case let .failure(authError) = $0 { print("Fetch user attributes failed with error \(authError)") } } receiveValue: { attributes in print("User attributes - \(attributes)") }}Update user attribute
Invoke the update api for creating new or updating existing user attributes.
func updateAttribute() { Amplify.Auth.update(userAttribute: AuthUserAttribute(.phoneNumber, value: "+2223334444")) { result in do { let updateResult = try result.get() switch updateResult.nextStep { case .confirmAttributeWithCode(let deliveryDetails, let info): print("Confirm the attribute with details send to - \(deliveryDetails) \(info)") case .done: print("Update completed") } } catch { print("Update attribute failed with error \(error)") } }}func updateAttribute() -> AnyCancellable { Amplify.Auth.update(userAttribute: AuthUserAttribute(.phoneNumber, value: "+2223334444")) .resultPublisher .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") } }}Verify user attribute
Some attributes require confirmation for the attribute update to complete. If the attribute need to be confirmed, the result of the above api will be CONFIRM_ATTRIBUTE_WITH_CODE. A confirmation code will be sent to the delivery medium mentioned in the delivery details. When the user gets the confirmation code, you can present a UI to the user to enter the code and invoke the confirm attribute api with their input:
func confirmAttribute() { Amplify.Auth.confirm(userAttribute: .email, confirmationCode: "390739") { result in switch result { case .success: print("Attribute verified") case .failure(let error): print("Update attribute failed with error \(error)") } }}func confirmAttribute() -> AnyCancellable { Amplify.Auth.confirm(userAttribute: .email, confirmationCode: "390739") .resultPublisher .sink { if case let .failure(authError) = $0 { print("Update attribute failed with error \(authError)") } } receiveValue: { _ in print("Attribute verified") }}Send user attribute verification code
If an attribute needs to be verified while the user is authenticated, invoke the api as shown below:
func resendCode() { Amplify.Auth.resendConfirmationCode(for: .email) { result in switch result { case .success(let deliveryDetails): print("Resend code send to - \(deliveryDetails)") case .failure(let error): print("Resend code failed with error \(error)") } }}func resendCode() -> AnyCancellable { Amplify.Auth.resendConfirmationCode(for: .email) .resultPublisher .sink { if case let .failure(authError) = $0 { print("Resend code failed with error \(authError)") } } receiveValue: { deliveryDetails in print("Resend code sent to - \(deliveryDetails)") }}