【发布时间】:2020-03-09 03:32:15
【问题描述】:
我的问题与this one 非常相似,因为我试图将authenticateUser SDK 方法包装在Promise 中并解决/拒绝它。
代码:
async function cognitoAuth(credentials, next, res) {
const userData = {
Username: credentials.email,
Pool: userPool
};
const authenticationDetails = getAuthenticationDetails(credentials);
let userCredentials;
let authenticatedUserResponse;
cognitoUser = new CognitoUser(userData);
try {
userCredentials = await checkIfAuthenticated(credentials.email);
if (userCredentials.UserStatus === CognitoUserStatus.FORCE_CHANGE_PASSWORD) {
res
.status(203)
.send(userCredentials);
} else if (userCredentials.UserStatus === CognitoUserStatus.CONFIRMED) {
authenticatedUserResponse = await authenticateUser(authenticationDetails);
console.log(authenticatedUserResponse);
}
} catch(err) {
if (err.message === CognitoErrorMessages.USER_NOT_EXIST) {
next({
name: err.message,
status: 404
});
}
}
}
如您所见,我有 2 个等待函数(checkIfAuthenticated 和 authenticateUser)。如果checkIfAuthenticated 抛出错误并拒绝承诺,那么catch 可以使用有效的err 对象。
但是,如果 authenticateUser 引发错误,则调用 catch 但未定义 err。
这是我的authenticateUser:
function authenticateUser(authenticationDetails) {
return new Promise((resolve, reject) => {
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: (result) => {
resolve(merge(err, {
res: "SUCCESS"
}));
},
onFailure: (err) => {
reject(merge(err, {
status: 401
}));
},
newPasswordRequired: (userAttrs, requiredAttrs) => {
resolve(merge(userAttrs, {
res: "NEW_PASS_REQ"
}));
}
});
});
}
使用断点,onFailure 被调用,它是正确的 err 对象,所以我不确定为什么它在 catch 中未定义
【问题讨论】:
标签: javascript node.js aws-sdk