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}
1func fetchAttributes() -> AnyCancellable {2 Amplify.Auth.fetchUserAttributes()3 .resultPublisher4 .sink {5 if case let .failure(authError) = $0 {6 print("Fetch user attributes failed with error \(authError)")7 }8 }9 receiveValue: { attributes in10 print("User attributes - \(attributes)")11 }12}
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}
1func updateAttribute() -> AnyCancellable {2 Amplify.Auth.update(userAttribute: AuthUserAttribute(.phoneNumber, value: "+2223334444"))3 .resultPublisher4 .sink {5 if case let .failure(authError) = $0 {6 print("Update attribute failed with error \(authError)")7 }8 }9 receiveValue: { updateResult in10 switch updateResult.nextStep {11 case .confirmAttributeWithCode(let deliveryDetails, let info):12 print("Confirm the attribute with details send to - \(deliveryDetails) \(info)")13 case .done:14 print("Update completed")15 }16 }17}
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}
1func confirmAttribute() -> AnyCancellable {2 Amplify.Auth.confirm(userAttribute: .email, confirmationCode: "390739")3 .resultPublisher4 .sink {5 if case let .failure(authError) = $0 {6 print("Update attribute failed with error \(authError)")7 }8 }9 receiveValue: { _ in10 print("Attribute verified")11 }12}
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}
1func resendCode() -> AnyCancellable {2 Amplify.Auth.resendConfirmationCode(for: .email)3 .resultPublisher4 .sink {5 if case let .failure(authError) = $0 {6 print("Resend code failed with error \(authError)")7 }8 }9 receiveValue: { deliveryDetails in10 print("Resend code sent to - \(deliveryDetails)")11 }12}