React Cognito用户池-客户端尝试写入未经授权的属性

时间:2018-11-26 06:00:31

标签: reactjs amazon-cognito aws-amplify aws-userpools

我在墙上敲头,试图弄清楚这个问题。我的应用程序中有以下代码。我需要为用户添加某种方式来添加单位/ apt号,所以我添加了自定义属性。

一切正常,但是当我包含apt_number: this.state.unitNumber,时,我得到了错误{code: "NotAuthorizedException", name: "NotAuthorizedException", message: "A client attempted to write unauthorized attribute"}

我确实进入设置并使属性可写(我尝试使用属性Unitapt number

这是我的代码:

const receivedNewUser = await Auth.signUp({
  username: this.state.email,
  password: this.state.password,
  attributes: {
    phone_number: this.state.phone,
    address: this.state.streetAddress,
    birthdate: this.state.dob,
    locale: this.state.zipCode,
    given_name: this.state.fname,
    family_name: this.state.lname,
    apt_number: this.state.unitNumber,
  },
});

这是怎么回事?

enter image description here

1 个答案:

答案 0 :(得分:2)

您需要在属性名称前添加custom:作为前缀。

您的代码应为:

const receivedNewUser = await Auth.signUp({
  username: this.state.email,
  password: this.state.password,
  attributes: {
    phone_number: this.state.phone,
    address: this.state.streetAddress,
    birthdate: this.state.dob,
    locale: this.state.zipCode,
    given_name: this.state.fname,
    family_name: this.state.lname,
    'custom:apt_number': this.state.unitNumber,
  },
});