首页 > 解决方案 > AWS Cognito 登录延迟太高

问题描述

我在我的应用程序中使用 aws cognito 进行注册和登录。我的应用程序后端托管在 aws lambda 上,并且位于我的 api-gateway 支持的 api 的前面。例如,我正在使用“amazon-cognito-identity-js”库并在后端从我的 nodejs lambda 调用注册。我公开的 api 是 /user/login 给用户。

我的后端 lambda 调用登录中的代码如下:

exports.signIn = (email, password) =>
    new Promise((resolve, reject) => {
        const authenticationDetails = new Cognito.AuthenticationDetails({
            Username: email,
            Password: password
        });

        const cognitoUser = new Cognito.CognitoUser({
            Username: email,
            Pool: userPool
        });

        cognitoUser.authenticateUser(authenticationDetails, {
            onSuccess: result => {
                resolve({
                    data: {
                        token: result.getIdToken().getJwtToken(),
                        refreshToken: result.getRefreshToken().getToken()
                    }
                })
            },
            onFailure: err => {
                console.log(err);
                reject({
                    data: {
                        errorCode: err.code,
                        errorMessage: err.message
                    },
                    statusCode: 400
                })
            }
        });
});

API 按预期工作正常,我能够取回令牌。

我面临的问题是这个 API 的高延迟。延迟大约为 5-8 秒,这太高了。我已经测试过,这种高延迟不能归因于 lambda 冷启动。

有人可以帮我理解为什么我的延迟这么高吗?

标签: node.jsauthenticationaws-lambdaamazon-cognito

解决方案


推荐阅读