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

Page updated May 17, 2024

Manage user attributes

User attributes such as email address, phone number help you identify individual users. Defining the user attributes you include for your user profiles makes user data easy to manage at scale. This information will help you personalize user journeys, tailor content, provide intuitive account control, and more. You can capture information upfront during sign-up or enable customers to update their profile after sign-up. In this section we take a closer look at working with user attributes, how to set them up and manage them.

Configure custom user attributes during sign-up

Custom attributes can be passed in with the userAttributes option of the signUp API:

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

Retrieve user attributes

You can retrieve user attributes for your users to read in their profile using the fetchUserAttributes API. This helps you personalize their frontend experience as well as control what they will see.

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

You can use the updateUserAttribute API to create or update 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 needs to be confirmed, part of the result of the updateUserAttribute or updateUserAttributes APIs will be confirmAttributeWithCode. 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 confirmUserAttribute 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 sendUserAttributeVerificationCode API as shown below:

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

Next Steps