1
votes

Hey everyone so I a have a predicament here. I am trying to add a custom attribute to my AWS cognito user but when ever I do I get the following error: {"message":"Invalid user attributes: custom:expiration_date: Attribute does not exist in the schema.\n","code":"InvalidParameterException","time":"2019-10-08T20:29:26.050Z","requestId":"e088a624-95a3-4d42-a8d2-e2029e41f890","statusCode":400,"retryable":false,"retryDelay":30.119738164581644} However I have the following attribute defined in my AWS user pool as shown in the screenshot. enter image description here

I have the following code to update the attribute but with no success.

router.put("/set-user-expiration-date", async (req, res) => {
  if (res.locals.user && res.locals.user.role === "admin") {
    try {
      const date = req.body.expirationDate.toString();
      const params = {
        UserAttributes: [{ Name: "custom:expiration_date", Value: date }],
        UserPoolId: process.env.AWS_COGNITO_USER_POOL_ID,
        Username: req.body.username
      };
      await cognito.adminUpdateUserAttributes(params).promise();
      res.send("Successfully updated.");
    } catch (error) {
      res.send(error);
    }
  } else {
    res.status(401).send("No admin rights");
  }
});

Any help here would be great. Thanks :)

2

2 Answers

0
votes

The problem was the character limit. I had way to many in the max and it caused it to error out. Once I created a new attribute and set the max to 256 it worked just fine.

0
votes

I found two things with for related to set custom attribute

1 custom attribute should be with 'custom:' prefix like below example. I have set the role attribute as custom attribute.

const AmazonCognitoIdentity = require('amazon-cognito-identity-js');
let cognitoAttributeList: any[] = [];

function setCognitoAttributeList(email: string, name: string, middle_name: string, role: string, agent: any) {
    let attributeList = [];
    attributeList.push(attributes('email', email));
    attributeList.push(attributes('name', name));
    attributeList.push(attributes('middle_name', middle_name));
    attributeList.push(attributes('custom:role', role));
    attributeList.forEach((element) => {
        cognitoAttributeList.push(new AmazonCognitoIdentity.CognitoUserAttribute(element));
    });
}

2 Go to AWS Console > User Pool > General settings > App Clients > Show details > Set attribute read and write permissions.

enter image description here

I found this realy valuable step via this github issue https://github.com/aws/aws-sdk-js/issues/1084#issuecomment-382699312