Authentication category of AWS Amplify providing APIs and building blocks for creating authentication experiences with Amazon Cognito
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
User profile operations including attribute management, verification, and account operations.
Retrieve information about the currently authenticated user.
function getCurrentUser(): Promise<AuthUser>;
interface AuthUser {
username: string;
userId: string;
signInDetails?: AuthSignInDetails;
}
interface AuthSignInDetails {
loginId?: string;
authFlowType?: AuthFlowType;
}import { getCurrentUser } from "@aws-amplify/auth";
try {
const user = await getCurrentUser();
console.log("Current user:", user.username);
console.log("User ID:", user.userId);
} catch (error) {
console.log("No user signed in");
}Get all attributes for the current user.
function fetchUserAttributes(): Promise<FetchUserAttributesOutput>;
type FetchUserAttributesOutput = Record<UserAttributeKey, string>;import { fetchUserAttributes } from "@aws-amplify/auth";
const attributes = await fetchUserAttributes();
console.log("User email:", attributes.email);
console.log("User name:", attributes.name);
console.log("Phone number:", attributes.phone_number);
// All available attributes
Object.entries(attributes).forEach(([key, value]) => {
console.log(`${key}: ${value}`);
});Update multiple user attributes at once.
function updateUserAttributes(input: UpdateUserAttributesInput): Promise<UpdateUserAttributesOutput>;
interface UpdateUserAttributesInput {
userAttributes: Record<UserAttributeKey, string>;
clientMetadata?: Record<string, string>;
}
interface UpdateUserAttributesOutput {
[key: UserAttributeKey]: {
isUpdated: boolean;
nextStep: {
updateAttributeStep: 'CONFIRM_ATTRIBUTE_WITH_CODE' | 'DONE';
codeDeliveryDetails?: CodeDeliveryDetails;
};
};
}import { updateUserAttributes } from "@aws-amplify/auth";
const result = await updateUserAttributes({
userAttributes: {
name: "John Updated",
email: "newemail@example.com",
phone_number: "+19876543210"
}
});
// Check which attributes need verification
Object.entries(result).forEach(([attribute, details]) => {
if (details.nextStep.updateAttributeStep === 'CONFIRM_ATTRIBUTE_WITH_CODE') {
console.log(`Verification code sent for ${attribute} to ${details.nextStep.codeDeliveryDetails?.destination}`);
} else {
console.log(`${attribute} updated successfully`);
}
});Update a single user attribute.
function updateUserAttribute(input: UpdateUserAttributeInput): Promise<UpdateUserAttributeOutput>;
interface UpdateUserAttributeInput {
userAttribute: {
attributeKey: UserAttributeKey;
value: string;
};
clientMetadata?: Record<string, string>;
}
interface UpdateUserAttributeOutput {
isUpdated: boolean;
nextStep: {
updateAttributeStep: 'CONFIRM_ATTRIBUTE_WITH_CODE' | 'DONE';
codeDeliveryDetails?: CodeDeliveryDetails;
};
}import { updateUserAttribute } from "@aws-amplify/auth";
const result = await updateUserAttribute({
userAttribute: {
attributeKey: 'email',
value: 'newemail@example.com'
}
});
if (result.nextStep.updateAttributeStep === 'CONFIRM_ATTRIBUTE_WITH_CODE') {
console.log(`Verification code sent to: ${result.nextStep.codeDeliveryDetails?.destination}`);
} else {
console.log("Email updated successfully");
}Confirm a user attribute change using a verification code.
function confirmUserAttribute(input: ConfirmUserAttributeInput): Promise<void>;
interface ConfirmUserAttributeInput {
userAttributeKey: VerifiableUserAttributeKey;
confirmationCode: string;
}import { confirmUserAttribute } from "@aws-amplify/auth";
await confirmUserAttribute({
userAttributeKey: 'email',
confirmationCode: '123456'
});
console.log("Email verified successfully");Send a verification code for a user attribute that requires verification.
function sendUserAttributeVerificationCode(input: SendUserAttributeVerificationCodeInput): Promise<SendUserAttributeVerificationCodeOutput>;
interface SendUserAttributeVerificationCodeInput {
userAttributeKey: VerifiableUserAttributeKey;
clientMetadata?: Record<string, string>;
}
interface SendUserAttributeVerificationCodeOutput {
destination?: string;
deliveryMedium?: 'EMAIL' | 'SMS';
attributeName?: string;
}import { sendUserAttributeVerificationCode } from "@aws-amplify/auth";
const codeDelivery = await sendUserAttributeVerificationCode({
userAttributeKey: 'phone_number'
});
console.log(`Verification code sent to: ${codeDelivery.destination}`);Delete user attributes from the user profile.
function deleteUserAttributes(input: DeleteUserAttributesInput): Promise<void>;
interface DeleteUserAttributesInput {
userAttributeKeys: UserAttributeKey[];
}import { deleteUserAttributes } from "@aws-amplify/auth";
await deleteUserAttributes({
userAttributeKeys: ['middle_name', 'nickname', 'website']
});
console.log("Attributes deleted successfully");Permanently delete the user account.
function deleteUser(): Promise<void>;import { deleteUser } from "@aws-amplify/auth";
// This is a destructive operation - consider showing confirmation dialog
const confirmDelete = confirm("Are you sure you want to delete your account? This action cannot be undone.");
if (confirmDelete) {
await deleteUser();
console.log("User account deleted successfully");
}Available user attribute keys and their purposes:
type UserAttributeKey =
| 'address' // Full mailing address
| 'birthdate' // Date of birth (YYYY-MM-DD)
| 'email' // Email address (verifiable)
| 'family_name' // Last name
| 'gender' // Gender
| 'given_name' // First name
| 'locale' // Locale (language/country)
| 'middle_name' // Middle name
| 'name' // Full name
| 'nickname' // Nickname
| 'phone_number' // Phone number (verifiable, E.164 format)
| 'picture' // Profile picture URL
| 'preferred_username'// Preferred username
| 'profile' // Profile page URL
| 'sub' // User identifier (read-only)
| 'updated_at' // Last update timestamp (read-only)
| 'website' // Website URL
| 'zoneinfo' // Time zone
| (string & {}); // Custom attributes
type VerifiableUserAttributeKey = 'email' | 'phone_number';import { updateUserAttribute, AuthError } from "@aws-amplify/auth";
try {
await updateUserAttribute({
userAttribute: {
attributeKey: 'email',
value: 'invalid-email'
}
});
} catch (error) {
if (error instanceof AuthError) {
switch (error.name) {
case 'InvalidParameterException':
console.log('Invalid email format');
break;
case 'NotAuthorizedException':
console.log('User not authorized');
break;
case 'AliasExistsException':
console.log('Email already in use by another account');
break;
default:
console.log('Update failed:', error.message);
}
}
}