Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Feb 21, 2024

Manage user attributes

Fetch the current user's attributes

Invoke the following api to get the list of attributes assigned to the user.

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)")
}
}

Update user attribute

Invoke the update api for creating new or updating existing user attributes.

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")
}
}
}

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() 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")
}
}

Send user attribute verification code

If an attribute needs to be verified while the user is authenticated, invoke the send api as shown below:

func resendCode() async {
do {
let deliveryDetails = try await Amplify.Auth.resendConfirmationCode(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 resendCode() -> AnyCancellable {
Amplify.Publisher.create {
try await Amplify.Auth.resendConfirmationCode(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)")
}
}