我得到错误:用户未经过身份验证更新用户属性时出错。如果这不是更新的方式,请分享更新的方式。我参考了https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_UpdateUserAttributes.html,但不明白。
更新代码
function update(username, password) {
try {
var attributeList = [];
attributeList.push(new AmazonCognitoIdentity.CognitoUserAttribute({
Name: "custom:role",
Value: "some new value"
}));
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails({
Username: username,
Password: password,
});
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var userData = {
Username: username,
Pool: userPool
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.updateAttributes(attributeList, (err, result) => {
if (err) {
console.log("nnnnUpdate Error: ", err, "nnn");
} else {
console.log(result);
}
});
} catch (e) {
console.log("nnnnCommon Error", e, "nnn");
}
}
错误在这里
Update Error: Error: User is not authenticated
at CognitoUser.updateAttributes (C:UsersKiranDocumentsProjectsRentals-Backendnode_modulesamazon-cognito-identity-jslibCognitoUser.js:1040:23)
at update (C:UsersKiranDocumentsProjectsRentals-Backendmodelsuser.js:94:21)
at C:UsersKiranDocumentsProjectsRentals-Backendmodelsuser.js:33:9
at new Promise (<anonymous>)
at Object.exports.signIn (C:UsersKiranDocumentsProjectsRentals-Backendmodelsuser.js:32:12)
at exports.signIn (C:UsersKiranDocumentsProjectsRentals-Backendcontrollersuser.js:12:11)
at Layer.handle [as handle_request] (C:UsersKiranDocumentsProjectsRentals-Backendnode_modulesexpresslibrouterlayer.js:95:5)
at next (C:UsersKiranDocumentsProjectsRentals-Backendnode_modulesexpresslibrouterroute.js:137:13)
at Route.dispatch (C:UsersKiranDocumentsProjectsRentals-Backendnode_modulesexpresslibrouterroute.js:112:3)
at Layer.handle [as handle_request] (C:UsersKiranDocumentsProjectsRentals-Backendnode_modulesexpresslibrouterlayer.js:95:5)
at C:UsersKiranDocumentsProjectsRentals-Backendnode_modulesexpresslibrouterindex.js:281:22
at Function.process_params (C:UsersKiranDocumentsProjectsRentals-Backendnode_modulesexpresslibrouterindex.js:335:12)
at next (C:UsersKiranDocumentsProjectsRentals-Backendnode_modulesexpresslibrouterindex.js:275:10)
at urlencodedParser (C:UsersKiranDocumentsProjectsRentals-Backendnode_modulesbody-parserlibtypesurlencoded.js:82:7)
at Layer.handle [as handle_request] (C:UsersKiranDocumentsProjectsRentals-Backendnode_modulesexpresslibrouterlayer.js:95:5)
at trim_prefix (C:UsersKiranDocumentsProjectsRentals-Backendnode_modulesexpresslibrouterindex.js:317:13)
at C:UsersKiranDocumentsProjectsRentals-Backendnode_modulesexpresslibrouterindex.js:284:7
at Function.process_params (C:UsersKiranDocumentsProjectsRentals-Backendnode_modulesexpresslibrouterindex.js:335:12)
at next (C:UsersKiranDocumentsProjectsRentals-Backendnode_modulesexpresslibrouterindex.js:275:10)
at C:UsersKiranDocumentsProjectsRentals-Backendnode_modulesbody-parserlibread.js:130:5
at invokeCallback (C:UsersKiranDocumentsProjectsRentals-Backendnode_modulesraw-bodyindex.js:224:16)
at done (C:UsersKiranDocumentsProjectsRentals-Backendnode_modulesraw-bodyindex.js:213:7)
at IncomingMessage.onEnd (C:UsersKiranDocumentsProjectsRentals-Backendnode_modulesraw-bodyindex.js:273:7)
at IncomingMessage.emit (events.js:375:28)
at IncomingMessage.emit (domain.js:470:12)
at endReadableNT (internal/streams/readable.js:1317:12)
at processTicksAndRejections (internal/process/task_queues.js:82:21)
在调用updateAttributes
之前应该对用户进行身份验证cognitoUser.authenticateUser(
https://docs.aws.amazon.com/cognito/latest/developerguide/authentication.html
// Amazon Cognito creates a session which includes the id, access, and refresh tokens of an authenticated user.
var authenticationData = {
Username : 'username',
Password : 'password',
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
var poolData = { UserPoolId : 'us-east-1_ExaMPle',
ClientId : '1example23456789'
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var userData = {
Username : 'username',
Pool : userPool
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
var accessToken = result.getAccessToken().getJwtToken();
/* Use the idToken for Logins Map when Federating User Pools with identity pools or when passing through an Authorization Header to an API Gateway Authorizer */
var idToken = result.idToken.jwtToken;
},
onFailure: function(err) {
alert(err);
},
});