Changlin An
Changlin An

Reputation: 215

Is it possible to update the userAttributes(email) in aws cognito using React-Native?

I have tried below lines to update the userEmail in aws cognito. For update email, the updateAttributes is needed.

import {Auth} from 'aws-amplify';


let user = await Auth.currentAuthenticatedUser();
user.session = user.signInUserSession;
let userAttributes = await Auth.userAttributes(user);

let new = { Name: 'email', Value: emailAddress }
Auth.updateUserAttributes(user, new).then((res) => {
   console.log("success", res);
}).catch(e => {
   console.log("EEE", e);
});

But when I tried, I got this error:

{"code": "InvalidParameterException", "message": "user.Value: Attribute does not exist in the schema.
user.Name: Attribute does not exist in the schema.
", "name": "InvalidParameterException"}

Upvotes: 1

Views: 1406

Answers (2)

Changlin An
Changlin An

Reputation: 215

Same page with me, I think.

const fields = {
   'email': '[email protected]'
};
async function updateUser(user, fields) {
  await Auth.updateUserAttributes(user, {
    ...fields
  });
}

Upvotes: 0

Engam
Engam

Reputation: 1061

You are almost there, the input should just be a normal key/value where the key is 'email' and the value is the emailadress.

let new = { email: emailAddress } // <-- change this line
Auth.updateUserAttributes(user, new).then((res) => {
   console.log("success", res);
}).catch(e => {
   console.log("EEE", e);
});

Normally when working with cognito with the aws-sdk you should use the syntax that you did, but it is diffrent with amplify.

This way you can edit more userAttributes at once. Take a look at det amplify docs for a more detailed look: https://docs.amplify.aws/lib/auth/manageusers/q/platform/js/#managing-user-attributes

Upvotes: 1

Related Questions