react-native aws 增强无密码身份验证(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)

import {Auth} from 'aws-amplify';

const onSignInPressed = async data => {
    try {
      const user = await Auth.signIn(phonenumber);
      setSession(user);
      setReady(true);
    } catch (e) {
      ...
    }
}

我的应用2(V6)

import {signIn} from 'aws-amplify/auth';

const onSignInPressed = async data => {
    try {
      const user = await signIn(phonenumber);
      const user = await signIn({username: phonenumber});
      const user = await signIn({username: phonenumber, password: null});
      const user = await signIn({username: phonenumber, password: pwd});
      setSession(user);
      setReady(true);
    } catch (e) {
      ...
    }
}

myApp1 工作正常。但是 myApp2 有以下错误。 四种方式调用“signIn”函数时出现如下错误。

const user = await signIn(phonenumber);
>> ERROR  [EmptySignInUsername: username is required to signIn]
const user = await signIn({username: phonenumber});
>> ERROR  [EmptySignInPassword: password is required to signIn]
const user = await signIn({username: phonenumber, password: null});
>> ERROR  [EmptySignInPassword: password is required to signIn]
const user = await signIn({username: phonenumber, password: pwd});
>> ERROR  [EmptySignInPassword: password is required to signIn]

我将后端配置为无需密码即可进行身份验证(myApp1),我按原样获取了配置并实现了 myApp2,但我不知道为什么要求我输入密码。 (myApp1 仍然工作正常。)

唯一的区别是版本。(V5和V6)

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

谢谢你们。

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

随着最近对 AWS Amplify 库版本 6 的更新,身份验证流程似乎发生了变化。要解决您面临的问题,您需要在调用 Amplify

signIn
中的
authFlowType
函数时将
'CUSTOM_WITHOUT_SRP'
显式指定为 signIn。此更改对于与版本 6 中引入的更新的身份验证流程保持一致是必要的。

以下是如何修改 myApp2 中的

onSignInPressed
函数的示例:

import { signIn } from 'aws-amplify/auth';

const onSignInPressed = async (data) => {
  try {
    const user = await signIn({
      username: phonenumber,
      options: {
        authFlowType: 'CUSTOM_WITHOUT_SRP',
      },
    });

    // Continue with your logic based on the user response.

    setSession(user);
    setReady(true);
  } catch (e) {
    // Handle any errors here.
    console.error(e);
  }
};

通过显式将

authFlowType
设置为
'CUSTOM_WITHOUT_SRP'
,您应该能够启动您之前定义的自定义身份验证质询 Lambda 触发器。有关自定义身份验证质询 Lambda 触发器的更多详细信息,您可以参阅AWS 文档。此资源提供了有关如何在用户池挑战的背景下配置和使用 Lambda 触发器的深入信息。

此外,请确保您的 Cognito 用户池配置包含登录期间使用的参数的必要设置。您可以参阅有关配置 Amazon Cognito 的 Amplify 文档了解更多详细信息:配置 Amazon Cognito

如果您有任何其他疑问或遇到任何问题,请随时与我们联系。祝您发展顺利!

© www.soinside.com 2019 - 2024. All rights reserved.