react-native aws amplify (V6) 自定义身份验证质询(无密码)

问题描述 投票:0回答:1

我用react-native和Amplify开发了一个应用程序(这是myApp1)。我使用 AWS Amplify 实施了无密码身份验证。 (与 https://techinscribed.com/passwordless-phone-number-authentication-using-aws-amplify-cognito/ 不完全相同,但类似)

现在,我正在使用react-native创建一个新应用程序(这是myApp2)并存在放大后端(使用前一个)。

当我开发myApp1时,Amplify库是V5,但现在是V6。所以,myApp1是由V5开发的,myApp2是由V6开发的。

下面是我的代码。

我的应用1(V5)

const onSignInPressed = async data => {
    setIsLoading(true);
    const username = data.username;
    try {
      const user = await Auth.signIn('+91' + username);
      setSession(user);
      setReady(true);
    } catch (e) {
      console.error(e);
    } finally {
      setIsLoading(false);
    }
  };

  const onVerifyOtp = async data => {
    setIsLoading(true);
    try {
      const user = await Auth.sendCustomChallengeAnswer(session, data.otp);
      setAuthUser(user);
    } catch (e) {
      console.error(e);
    } finally {
      setIsLoading(false);
    }
  };

单击“登录”按钮时,会发送 OTP,输入 OTP 后,单击“验证 OTP”按钮会使用 sendCustomChallengeAnswer 进行登录。

我参考AWS文档修改了我的代码如下。 (V6) (https://docs.amplify.aws/react-native/build-a-backend/auth/switch-auth/#pageMain

它说“要在应用程序中启动自定义身份验证流程,请在没有密码的情况下调用登录”。需要使用确认登录来回答自定义挑战

我的应用2(V6)

const onSignInPressed = async () => {
    setIsLoading(true);
    try {
      const user = await signIn({
        username: '+91' + phoneNumber,
        options: {
          authFlowType: 'CUSTOM_WITHOUT_SRP',
        },
      });

      setSession(user);
      setReady(true);
    } catch (err) {
      console.error(err);
    } finally {
      setIsLoading(false);
    }
  };

const onVerifyOtp = async (otp) => {
    setIsLoading(true);
    try {
      console.log(otp);
      console.log(session.nextStep.signInStep);
      const output = await confirmSignIn({otp});
      console.log(output);

      setDbUser(output);
    } catch (err) {
      console.error('onVerifyOtp', err);
    } finally {
      setIsLoading(false);
    }
  };

但是我遇到了以下错误。

 LOG  111111
 LOG  CONFIRM_SIGN_IN_WITH_CUSTOM_CHALLENGE
 ERROR  onVerifyOtp [EmptyChallengeResponse: challengeResponse is required to confirmSignIn]

如果有人有解决方案,我将非常感谢详细的解释。 (我很新,如果你能详细解释一下,我将非常感激。)

谢谢你。

react-native aws-amplify amplifyjs amplify-auth-cognito
1个回答
0
投票

这个问题是由于参数名称造成的,如果将其作为challengeResponse传递到confirmSignIn函数中,效果很好,如下所示。

const challengeResponse = otp;
const user = await confirmSignIn({challengeResponse});
© www.soinside.com 2019 - 2024. All rights reserved.