如何获取和更新用户属性AWS Amplify Angle 6

时间:2018-12-06 14:08:19

标签: angular amazon-web-services authentication amazon-cognito aws-amplify

注册到应用程序后,如何更改Cognito用户的属性?应该使用什么API来获取用户的详细信息,例如名字,姓氏等? 应该使用什么API更新用户详细信息?

1 个答案:

答案 0 :(得分:4)

要访问fetch the details of the user,只需使用this.amplifyService.auth().currentAuthenticatedUser()并检索user.attributes字段。

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

对于update the attributes,请使用updateUserAttributes方法。

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

如果您需要检索CognitoUser,则可以遵循文档的Change password example

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