代码之家  ›  专栏  ›  技术社区  ›  wmash

AWS Cognito返回未定义的错误async/await

  •  0
  • wmash  · 技术社区  · 7 年前

    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对象,因此我不确定为什么在

    1 回复  |  直到 7 年前
        1
  •  0
  •   wmash    7 年前

    我明白了。我的一位同事指出,我在服务器代码上运行babel是因为在我的 index.js (我的服务器入口点) :

    require("@babel/polyfill");
    require("@babel/register");
    

    节点中的一个透明问题。当我把它拿出来并切换到ES5进口/出口时,它工作得很好