如何解决Cognito中的“客户端尝试写入未经授权的属性”问题?

时间:2019-01-11 08:23:35

标签: node.js aws-sdk amazon-cognito

我在Cognito中遇到signUp问题,有时显示如下错误:

{
  "code": "NotAuthorizedException",
  "name": "NotAuthorizedException",
  "message": "A client attempted to write unauthorized attribute"
}

如果我重新部署代码,则该问题将自动修复,或者有时无需任何部署即可自动修复。我使用3个自定义属性和2个属性(名称,电子邮件),所有这些属性均已授权给我的cognito客户端。这是我的代码

function signUp(user) {
let userData = {
    Username: user.email,
    Pool: userPool
};
let cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
var dataEmail = {
    Name: 'email',
    Value: user.email
};
var dataName = {
    Name: 'name',
    Value: user.first_name
};
var dataLName = {
    Name: 'custom:last_name',
    Value: user.last_name
};

var dataFName = {
    Name: 'custom:first_name',
    Value: user.first_name
};
var dataRealm = {
    Name: 'custom:type',
    Value: user.type
};

var attributeEmail = new AmazonCognitoIdentity.CognitoUserAttribute(dataEmail);
var attributeName = new AmazonCognitoIdentity.CognitoUserAttribute(dataName);
var attributeRealm = new AmazonCognitoIdentity.CognitoUserAttribute(dataRealm);
var attributeLastName = new AmazonCognitoIdentity.CognitoUserAttribute(dataLName);
var attributeFirstName = new AmazonCognitoIdentity.CognitoUserAttribute(dataFName);

attributeList.push(attributeEmail);
attributeList.push(attributeName);
attributeList.push(attributeRealm);
attributeList.push(attributeLastName);
attributeList.push(attributeFirstName);

if (user.type == 2) {
    userPool.signUp(email, password, attributeList, null, (err, result) => {
        if (err) {
            console.log("Error in  createUser : cognito_helper", err);
            console.log(err)
            reject(err);
        } else {
            resolve(result);
        }
    })
} else {
    let emailVerified = {
        Name: 'email_verified',
        Value: "false"
    }
    var attributeEmailVerified = new AmazonCognitoIdentity.CognitoUserAttribute(emailVerified);
    attributeList.push(attributeEmailVerified);
    let password = "*******"
    let tempPassword = crypto.createHash('md5').update(password).digest('hex').substring(0, 17);
    console.log(tempPassword)
    let params = {
        UserPoolId: config.awsConfiguration.cognitoUserPoolId,
        Username: email,`enter code here`
        DesiredDeliveryMediums: [
            "EMAIL"
        ],
        UserAttributes: attributeList,
        TemporaryPassword: tempPassword
    };
    cognitoidentityserviceprovider.adminCreateUser(params, function (err, result) {
        if (err) {
            console.log("Error in  createUser : cognito_helper", err);
            reject(err);
        } else {
            resolve(result)
        }
    });
}

}

使用adminCreateUser创建用户,仅使用signUp创建用户时未显示问题

谢谢。

0 个答案:

没有答案