javascript - AWS Cognito and Amplify: clientMetadata not sent when session is refreshed - Stack Overflow

We have a React client that uses AWS Cognito and Amplify ("aws-amplify": "1.1.40").

We have a React client that uses AWS Cognito and Amplify ("aws-amplify": "1.1.40").

When a user logs in we want to send some additional data to Cognito, to be used by a "pre token generation" trigger. We do this by adding a clientMetadata ({"metadataKey1": "metadataValue1"}) object to the Auth.signIn function call:

Auth.signIn(auth.email, auth.password, {"metadataKey1": "metadataValue1"})
    .then(response => {
        // Sign in OK
    })
    .catch(error => {
        // Something went wrong
    });

This works as expected, and the Cognito "pre token generation" lambda can extract the "metadataKey1" from the clientMetadata in the request.

This is where our problem starts:

After the successful signIn, AWS Amplify automatically does a session refresh. This session refresh is not explicitly done by our code, and the clientMetadata object used during signIn is not set. This of course means that the automatic session refresh request to Cognito does not contain the clientMetadata, which in turn means that the Cognito "pre token generation" lambda can not extract "metadataKey1" from the clientMetadata in the request (as it does not exist).

We have debugged the code, and found that the automatic request to Cognito happens in @aws-amplify\auth\node_modules\amazon-cognito-identity-js\es\CognitoUser.js#1249, CognitoUser.prototype.refreshSession. The refreshSession function can receive a clientMetadata object, but when debugging the code the clientMetadata object is always undefined (which makes sense; we have not set it explicitly and the Amplify code does not seem to store/use the clientMetadata we set during signIn).

What we need help with:

  1. Are we not doing enough? Do we have to do some other things in our code to make sure the clientMetadata object is sent on every request to Cognito, even requests that are not explicitly done by our code?
  2. Are we doing it wrong? The goal is to make sure we can send our own data on every request to Cognito. Are there other ways to do this than use the clientMetadata object?

Would really appreciate any help with this!

We have a React client that uses AWS Cognito and Amplify ("aws-amplify": "1.1.40").

When a user logs in we want to send some additional data to Cognito, to be used by a "pre token generation" trigger. We do this by adding a clientMetadata ({"metadataKey1": "metadataValue1"}) object to the Auth.signIn function call:

Auth.signIn(auth.email, auth.password, {"metadataKey1": "metadataValue1"})
    .then(response => {
        // Sign in OK
    })
    .catch(error => {
        // Something went wrong
    });

This works as expected, and the Cognito "pre token generation" lambda can extract the "metadataKey1" from the clientMetadata in the request.

This is where our problem starts:

After the successful signIn, AWS Amplify automatically does a session refresh. This session refresh is not explicitly done by our code, and the clientMetadata object used during signIn is not set. This of course means that the automatic session refresh request to Cognito does not contain the clientMetadata, which in turn means that the Cognito "pre token generation" lambda can not extract "metadataKey1" from the clientMetadata in the request (as it does not exist).

We have debugged the code, and found that the automatic request to Cognito happens in @aws-amplify\auth\node_modules\amazon-cognito-identity-js\es\CognitoUser.js#1249, CognitoUser.prototype.refreshSession. The refreshSession function can receive a clientMetadata object, but when debugging the code the clientMetadata object is always undefined (which makes sense; we have not set it explicitly and the Amplify code does not seem to store/use the clientMetadata we set during signIn).

What we need help with:

  1. Are we not doing enough? Do we have to do some other things in our code to make sure the clientMetadata object is sent on every request to Cognito, even requests that are not explicitly done by our code?
  2. Are we doing it wrong? The goal is to make sure we can send our own data on every request to Cognito. Are there other ways to do this than use the clientMetadata object?

Would really appreciate any help with this!

Share Improve this question asked Jun 24, 2020 at 6:18 bhmbhm 1711 silver badge3 bronze badges 4
  • 1 same problem here , found solution yet ? – Jimmy Obonyo Abor Commented Sep 3, 2020 at 23:34
  • 4 Hello Jimmy, no we have not found a solution to this actual problem. We have changed our architecture and managed to work around this problem instead. – bhm Commented Sep 6, 2020 at 7:50
  • 1 i thanks for update , raised an issue here github./aws-amplify/amplify-js/issues/6731 , lets see if they get to resolve it. – Jimmy Obonyo Abor Commented Sep 8, 2020 at 22:30
  • @bhm what workaround you used ? – Yogeshwar Tanwar Commented Sep 15, 2021 at 19:30
Add a ment  | 

1 Answer 1

Reset to default 2

According to API Reference, The ClientMetadata value is passed as input to the functions for ONLY the following triggers:

  • Pre signup
  • Pre authentication
  • User migration

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745100115a4611222.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信