Javascript AdminListGroupsForUser返回的用户不存在
我的应用程序是一个使用2个服务进行身份验证的过程,其中一个是AWS Cognito。当用户经过身份验证时(Cognito提供的JWT令牌也是如此),我尝试通过Javascript AdminListGroupsForUser返回的用户不存在,javascript,aws-sdk,amazon-cognito,Javascript,Aws Sdk,Amazon Cognito,我的应用程序是一个使用2个服务进行身份验证的过程,其中一个是AWS Cognito。当用户经过身份验证时(Cognito提供的JWT令牌也是如此),我尝试通过AdminListGroupsForUserAPI调用列出该用户的组。我得到了UserNotFoundException,这很奇怪,因为上一次调用刚刚用相同的凭据验证了该用户 我尝试了以下几点: router.post("/groups/list", (req, res, next) => { const { email, l
AdminListGroupsForUser
API调用列出该用户的组。我得到了UserNotFoundException,这很奇怪,因为上一次调用刚刚用相同的凭据验证了该用户
我尝试了以下几点:
router.post("/groups/list", (req, res, next) => {
const { email, limit, nextToken } = req.body;
const listGroupsForUserParams = getAdminListGroupsForUserParams({
email,
limit,
nextToken
});
const getUserParams = getAdminGetUserParams(email);
cognitoClient.adminListGroupsForUser(listGroupsForUserParams, (listErr, listData) => {
cognitoClient.adminGetUser(getUserParams, (getErr, getData) => {
console.log(listErr); // "UserNotFoundException"
console.log(listData); // null
console.log(getErr); // null
console.log(getData); // User
});
});
});
listgroupsforserparams
和getUserParams
包含相同的信息,即:
{
UserPoolId: "...",
Username: "test@example.com" // I use email as Username
}
我不明白为什么前一个调用在池中找不到用户,而后一个可以
见(参考文献):
adminListGroupsForUser
函数不接受电子邮件作为用户名,而adminGetUser
则接受。
我通过使用adminGetUser
检索用户数据来解决这个问题。它返回用户及其所有属性。检索名为sub
的属性值,并将其用作adminListGroupsForUser
调用的用户名
大概是这样的:
const getParams = {
UserPoolId: "" /*put your user pool Id here*/,
Username: "" /* email */
};
cognitoidentityserviceprovider.adminGetUser(getParams, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
return;
}
var sub;
if (data.UserAttributes && data.UserAttributes.length) {
for (var i = 0; i < data.UserAttributes.length; ++i) {
const attr = data.UserAttributes[i];
if (attr.Name === 'sub') {
console.log(attr);
sub = attr.Value
break;
}
}
}
if (!sub)
return;
const groupsParams = {
UserPoolId: event.userPoolId,
Username: sub
};
cognitoidentityserviceprovider.adminListGroupsForUser(groupsParams, function(err, data) {
/* Your code using groups here */
});
});
const getParams={
UserPoolId:“/*将您的用户池Id放在此处*/,
用户名:“/*电子邮件*/
};
cognitoidentityserviceprovider.adminGetUser(getParams,函数(err,数据){
如果(错误){
console.log(err,err.stack);//发生错误
返回;
}
var-sub;
if(data.UserAttributes&&data.UserAttributes.length){
对于(var i=0;i
您的cognitoClient的区域设置正确吗?@Deepthi是的,区域设置正确。我想如果不是这样,即使第二次调用adminGetUser
也会失败