User attribute management including CRUD operations, verification, and attribute-specific workflows.
Represents a user attribute with name-value pairs.
/**
* Represents a user attribute
* @param data - Attribute data with name and value
*/
class CognitoUserAttribute {
constructor(data: ICognitoUserAttributeData);
/** Attribute name */
Name: string;
/** Attribute value */
Value: string;
/** Returns the attribute value */
getValue(): string;
/** Sets the attribute value (chainable) */
setValue(value: string): CognitoUserAttribute;
/** Returns the attribute name */
getName(): string;
/** Sets the attribute name (chainable) */
setName(name: string): CognitoUserAttribute;
/** Returns JSON string representation */
toString(): string;
/** Returns object representation */
toJSON(): object;
}
interface ICognitoUserAttributeData {
/** Attribute name (e.g., 'email', 'given_name') */
Name: string;
/** Attribute value */
Value: string;
}Methods for managing user attributes on the CognitoUser class.
/**
* Gets all user attributes
* @param callback - Callback with array of user attributes
*/
getUserAttributes(callback: NodeCallback<Error, CognitoUserAttribute[]>): void;
/**
* Updates user attributes
* @param attributes - Array of attributes to update
* @param callback - Callback with update result and delivery details
* @param clientMetadata - Optional client metadata for Lambda triggers
*/
updateAttributes(
attributes: (CognitoUserAttribute | ICognitoUserAttributeData)[],
callback: UpdateAttributesNodeCallback<Error, string, any>,
clientMetadata?: ClientMetadata
): void;
/**
* Deletes specified user attributes
* @param attributeList - Array of attribute names to delete
* @param callback - Callback with deletion result
*/
deleteAttributes(
attributeList: string[],
callback: NodeCallback<Error, string>
): void;
/**
* Gets verification code for an attribute
* @param attributeName - Name of attribute to verify
* @param callbacks - Callback handlers for the verification flow
* @param clientMetadata - Optional client metadata for Lambda triggers
*/
getAttributeVerificationCode(
attributeName: string,
callbacks: {
onSuccess: (success: string) => void;
onFailure: (err: Error) => void;
inputVerificationCode?: (data: string) => void | null;
},
clientMetadata?: ClientMetadata
): void;
/**
* Verifies an attribute with confirmation code
* @param attributeName - Name of attribute to verify
* @param confirmationCode - Verification code received
* @param callbacks - Success/failure callback handlers
*/
verifyAttribute(
attributeName: string,
confirmationCode: string,
callbacks: {
onSuccess: (success: string) => void;
onFailure: (err: Error) => void;
}
): void;Usage Examples:
import { CognitoUserAttribute } from "amazon-cognito-identity-js";
// Create user attributes
const attributes = [
new CognitoUserAttribute({ Name: "given_name", Value: "John" }),
new CognitoUserAttribute({ Name: "family_name", Value: "Doe" }),
new CognitoUserAttribute({ Name: "email", Value: "john.doe@example.com" })
];
// Get user attributes
cognitoUser.getUserAttributes((err, attributes) => {
if (err) {
console.error("Get attributes failed:", err);
return;
}
attributes.forEach(attr => {
console.log(`${attr.getName()}: ${attr.getValue()}`);
});
});
// Update attributes
const updatedAttributes = [
new CognitoUserAttribute({ Name: "phone_number", Value: "+1234567890" })
];
cognitoUser.updateAttributes(updatedAttributes, (err, result, details) => {
if (err) {
console.error("Update failed:", err);
return;
}
console.log("Update successful:", result);
if (details) {
console.log("Verification required:", details);
}
});
// Verify attribute
cognitoUser.getAttributeVerificationCode("email", {
onSuccess: (data) => {
console.log("Verification code sent:", data);
},
onFailure: (err) => {
console.error("Failed to send code:", err);
},
inputVerificationCode: (data) => {
const code = prompt("Enter verification code:");
cognitoUser.verifyAttribute("email", code, {
onSuccess: (result) => {
console.log("Email verified:", result);
},
onFailure: (err) => {
console.error("Verification failed:", err);
}
});
}
});Common Cognito user pool attributes:
email - Email addressemail_verified - Email verification status (boolean)phone_number - Phone number (E.164 format)phone_number_verified - Phone verification status (boolean)given_name - First/given namefamily_name - Last/family namename - Full namepreferred_username - Preferred usernamepicture - Profile picture URLwebsite - Website URLgender - Genderbirthdate - Birth date (YYYY-MM-DD)locale - Locale preferenceupdated_at - Last update timestampCustom attributes must be prefixed with custom::
const customAttributes = [
new CognitoUserAttribute({ Name: "custom:department", Value: "Engineering" }),
new CognitoUserAttribute({ Name: "custom:employee_id", Value: "EMP12345" })
];