bhm
bhm

Reputation: 171

AWS Cognito and Amplify: clientMetadata not sent when session is refreshed

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!

Upvotes: 17

Views: 5943

Answers (1)

Edgar Zagórski
Edgar Zagórski

Reputation: 1519

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

Upvotes: 2

Related Questions