首页 > 解决方案 > 通过使用 Cognito 和 Amplify 发送新的 SMS MFA 来更新会话

问题描述

我有一个带有 Amplify 的 React 应用程序,它使用 AWS Cognito 池正确发送 SMS 以进行用户验证和身份验证。

登录顺利,用户收到短信。

当我尝试为需要很长时间才能进入 OTP 的用户实施“发送新短信”功能时,问题就出现了,并且会话在他们身上到期(3 分钟)。

以下是我的正常登录功能:

async componentDidMount() {
  
    const username = "yyy";
    const password = "zzz";

    user = await Auth.signIn(username, password);

    const { challengeName } = user;

    console.log(user);

    if (!challengeName) {
        const { history } = this.props;
        history.push("/", { err: "No current user" });
    }
}

这是我的“发送新短信”功能:

resendConfirmationCode = async (e) => {
    
    try {
        await Auth.resendSignUp(this.state.username);
        console.log("code resent successfully");
        this.resetTimer();
    } catch (err) {
        console.log("error resending code: ", err);
    }
};

这确实会发送另一条短信,但我会收到与第一条短信相同的错误消息:

"NotAuthorizedException", message: "Invalid session for the user, session is expired."

我也尝试刷新会话,我收到一条新短信,但它会导致相同的错误消息:

testingSessionTwo = async (e) => {
    try {
        const cognitoUser = await Auth.currentAuthenticatedUser();
        const currentSession = cognitoUser.signInUserSession;
        cognitoUser.refreshSession(currentSession.refreshToken, (err, session) => {
            
            Auth.signOut();
            
            const username = "sss";
            const password = "xxx";

            Auth.signIn(username, password);
            console.log(session);
        });
    } catch (e) {
        // whatever
    }
};

我在这里想念什么?

标签: reactjsamazon-cognitoaws-amplify

解决方案


推荐阅读