AWS Cognito 返回未定义错误 async/await
AWS Cognito returning undefined error async/await
我的问题与 非常相似,因为我试图将 authenticateUser
SDK 方法包装在 Promise
和 resolve/reject 中。
代码:
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
被调用,它是正确的错误对象,所以我不确定为什么它在 catch
中未定义
想通了。我的一位同事指出,我 运行 通过在我的 index.js
(我的服务器入口点):
require("@babel/polyfill");
require("@babel/register");
Node.js 中的转译问题。一旦我把它拿出来并切换到 ES5 imports/exports 它工作正常
我的问题与 authenticateUser
SDK 方法包装在 Promise
和 resolve/reject 中。
代码:
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
被调用,它是正确的错误对象,所以我不确定为什么它在 catch
想通了。我的一位同事指出,我 运行 通过在我的 index.js
(我的服务器入口点):
require("@babel/polyfill");
require("@babel/register");
Node.js 中的转译问题。一旦我把它拿出来并切换到 ES5 imports/exports 它工作正常