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
});
}
}
}
正如你所看到的,我有两个等待的函数
(
checkIfAuthenticated
)
核实
抛出错误并拒绝承诺,然后
catch
err
对象
然而,如果
认证者
抛出一个错误,则
接住
被调用,但
犯错误
这是我的
:
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对象,因此我不确定为什么在