Amplify-js: How to update user pool attribute using token

Created on 30 Aug 2018  路  5Comments  路  Source: aws-amplify/amplify-js

I want to update user pool attribute using node js . i have been already authenticate user and get access_token,id_token, refresh_token . So i want to know that is any way to authenticate user using these token .And after update atributes using ( cognitoUser.updateAttributes ) method previous token got invalid .
So how to get new Token ?

Here is code sample :
var obj = ({ Username : reqParam.email, Password : reqParam.password })
var attributeList = [];
attributeList.push(new AmazonCognitoIdentity.CognitoUserAttribute({Name:"name",Value: reqParam.name}));

        attributeList.push(new AmazonCognitoIdentity.CognitoUserAttribute({Name:"picture",Value: reqParam.picture?reqParam.picture:''}));

         var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(obj);
          var userData = {
              Username : reqParam.email,
              Pool : GetNewUserPool()
          };
            var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
            cognitoUser.authenticateUser(authenticationDetails, {
              onSuccess: function (authresult) {
                cognitoUser.updateAttributes(attributeList, function(err, result) {
            if (err) {
              console.log("err",err);
                reject(err);

            } else {
              console.log({
                access_token: authresult.getAccessToken().getJwtToken()
               ,id_token: authresult.getIdToken().getJwtToken()
               ,refresh_token: authresult.getRefreshToken().getToken()
               //,user_data: result.getUserAttributes()
               ,email:authresult.getUserAttributes()['email']
             })
              console.log("resil",result);
                resolve(result);
            }
        });
              },
              onFailure: function(err) {
                console.log("Err", err);
               reject(err);
              }
            })
Auth Cognito investigating node.js pending-close-response-required

Most helpful comment

All 5 comments

Is it possible to update user pool attribute with access_token or id_token ? can anyone send sample code for this issue ?

I'm trying to do the same with Amplify on the react side:

let user = await Auth.currentAuthenticatedUser();
let result = await Auth.updateUserAttributes(user, {
    'custom:merchant_id': merchant_id,
});
console.log(result); // SUCCESS
user = await Auth.currentAuthenticatedUser();
console.log(user);

This works as expected but it will not update ID (identification) token used by Amplify...

Now that old ID token is sent the claims in there are not updated, server receives the old claims.

Is there any way to refresh / regenerate the id token?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This issue has been automatically closed because of inactivity. Please open a new issue if are still encountering problems.

Was this page helpful?
0 / 5 - 0 ratings