Manage user attributes
Fetch the current user's attributes
Invoke the following api to get the list of attributes assigned to the user.
1func fetchAttributes() {2 Amplify.Auth.fetchUserAttributes() { result in3 switch result {4 case .success(let attributes):5 print("User attributes - \(attributes)")6 case .failure(let error):7 print("Fetching user attributes failed with error \(error)")8 }9 }10}
Update user attribute
Invoke the update api for creating new or updating existing user attributes.
1func updateAttribute() {2 Amplify.Auth.update(userAttribute: AuthUserAttribute(.phoneNumber, value: "+2223334444")) { result in3 do {4 let updateResult = try result.get()5 switch updateResult.nextStep {6 case .confirmAttributeWithCode(let deliveryDetails, let info):7 print("Confirm the attribute with details send to - \(deliveryDetails) \(info)")8 case .done:9 print("Update completed")10 }11 } catch {12 print("Update attribute failed with error \(error)")13 }14 }15}
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:
1func confirmAttribute() {2 Amplify.Auth.confirm(userAttribute: .email, confirmationCode: "390739") { result in3 switch result {4 case .success:5 print("Attribute verified")6 case .failure(let error):7 print("Update attribute failed with error \(error)")8 }9 }10}
Resend verification code
If the code has expired or the user needs to resend the confirmation code, invoke the resend api as shown below:
1func resendCode() {2 Amplify.Auth.resendConfirmationCode(for: .email) { result in3 switch result {4 case .success(let deliveryDetails):5 print("Resend code send to - \(deliveryDetails)")6 case .failure(let error):7 print("Resend code failed with error \(error)")8 }9 }10}