Sarath
Sarath

Reputation: 376

How to get and update user attributes AWS amplify angular 6

How would I change the attributes of the Cognito user after sign up into the application? What API should be used to fetch the details of the user like First Name, Last Name etc? What API should be used to update the user details?

Upvotes: 5

Views: 10275

Answers (1)

Johan Rin
Johan Rin

Reputation: 1950

To fetch the details of the user, simply use this.amplifyService.auth().currentAuthenticatedUser() and retrieve the user.attributes fields.

/**
 * Get current authenticated user
 * @return - A promise resolves to curret authenticated CognitoUser if success
 */
currentAuthenticatedUser(): Promise<CognitoUser | any>;

To update the attributes, use the updateUserAttributesmethod.

/**
 * Update an authenticated users' attributes
 * @param {CognitoUser} - The currently logged in user object
 * @return {Promise}
 **/
updateUserAttributes(user: CognitoUser | any, attributes: object): Promise<string>;

If you need to retrieve the CognitoUser, you can follow the Change password example of the documentation :

import { Auth } from 'aws-amplify';

Auth.currentAuthenticatedUser()
    .then(user => {
        return Auth.changePassword(user, 'oldPassword', 'newPassword');
    })
    .then(data => console.log(data))
    .catch(err => console.log(err));

Upvotes: 7

Related Questions