How to get and update user attributes AWS amplify angular 6

三世轮回 提交于 2019-12-06 07:47:32

问题


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?


回答1:


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));


来源:https://stackoverflow.com/questions/53653219/how-to-get-and-update-user-attributes-aws-amplify-angular-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!