首页 > 解决方案 > 用户在 AWS 用户池中更改密码时未通过身份验证

问题描述

当我当时尝试调用更改密码 API 时,我在 cognito 用户池中收到 User not authenticated 错误。

标签: ionic3

解决方案


嗨,我也为此苦苦挣扎,终于得到了这个解决方案。

您应该获得经过身份验证的用户

export const GetAuthenticatedUser = () => {
    const userPool = new CognitoUserPool({
        UserPoolId: process.env.OGNITO_POOL_ID,
        ClientId: process.env.COGNITO_CLIENT_ID
    });
    return userPool.getCurrentUser();
};

然后你应该调用 getSession() 方法并尝试在其回调中更改密码,否则它不起作用。

 export const ChangePassword = (data) => {
    return new Promise((resolve, reject) => {
        const currentUser = GetAuthenticatedUser();
        if (currentUser) {
            currentUser.getSession(function (err, session) {
                if (err) {
                    reject(err);
                } else {
                    currentUser.changePassword(data.old_password, data.password, function (
                        err,
                        res
                    ) {
                        if (err) {
                            reject(GetCognitoError(err.code + '_ChangePassword'));
                        } else {
                            resolve(res);
                        }
                    });
                }
            });
        } else {
            reject('User is not authenticated')
        }
    });
};

推荐阅读