Flutter + Firebase - 电话身份验证 OTP 问题

时间:2021-03-31 10:44:02

标签: firebase flutter firebase-authentication

我试图通过发送 OTP 并要求用户在收到 OTP 后输入该 OTP 来验证电话号码。此基于以下代码的功能在 iOS 模拟器上运行良好,但不适用于 Android。

<dependencies>
    <dependency>
        <groupId>io.helidon.microprofile.bundles</groupId>
        <artifactId>helidon-microprofile-1.2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-binding</artifactId>
    </dependency>
    <dependency>
        <groupId>org.jboss</groupId>
        <artifactId>jandex</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>javax.activation-api</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.4.3.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.1.4.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>el-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>javax.el</artifactId>
        <version>2.2.6</version>
    </dependency>
    <dependency>
        <groupId>com.oracle.ojdbc</groupId>
        <artifactId>ojdbc8</artifactId>
        <version>19.3.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.oracle.jdbc</groupId>
        <artifactId>oraclepki</artifactId>
        <version>18.3.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.oracle.jdbc</groupId>
        <artifactId>osdt_core</artifactId>
        <version>18.3.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.oracle.jdbc</groupId>
        <artifactId>osdt_cert</artifactId>
        <version>18.3.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
    </dependency>
    <dependency>
        <groupId>io.helidon.media</groupId>
        <artifactId>helidon-media-multipart</artifactId>
    </dependency>
    <dependency>
        <groupId>io.helidon.webserver</groupId>
        <artifactId>helidon-webserver</artifactId>
    </dependency>
    <dependency>
        <groupId>io.helidon.media</groupId>
        <artifactId>helidon-media-common</artifactId>
    </dependency>
    <dependency>
        <groupId>io.helidon.common</groupId>
        <artifactId>helidon-common</artifactId>
    </dependency>
    <dependency>
        <groupId>io.helidon.webclient</groupId>
        <artifactId>helidon-webclient</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.helidon.webserver</groupId>
        <artifactId>helidon-webserver</artifactId>
    </dependency>
    <dependency>
        <groupId>io.helidon.media.jackson</groupId>
        <artifactId>helidon-media-jackson-common</artifactId>
    </dependency>
</dependencies>

为了使其在 android 上运行,我在 Firebase 控制台中添加了 SHA1 和 SHA256。我还启用了 Android DeviceCheck API。

在 android 上,当我在文本字段中输入电话号码后按下一步时,它似乎尝试打开浏览器进行重新验证,但没有任何显示,只是移动到下一个屏幕要求 OTP。所以我假设它正在做隐形的recaptcha。

调试控制台显示以下错误:

verifyPhoneNumber() async {
    await auth.verifyPhoneNumber(
      phoneNumber: '+92${widget.phoneNumber}',
      verificationCompleted: (PhoneAuthCredential credential) async {
        await auth.currentUser.updatePhoneNumber(credential);
      },
      verificationFailed: (FirebaseAuthException e) {
        print(e.message);
      },
      codeSent: (String verificationId, int resendToken) {
        setState(() {
          _otpcode = verificationId;
        });
      },
      codeAutoRetrievalTimeout: (String verificationId) {
        setState(() {
          _otpcode = verificationId;
        });
      },
    );
  }

如上所述,相同的代码在 iOS 上运行良好,但由于某些奇怪的原因在 Android 上运行不正常。请让我知道我可能会遗漏什么。谢谢。

1 个答案:

答案 0 :(得分:0)

firebase otp 在模拟器上不起作用,请通过构建 apk 在真实设备上尝试或从 firebase 控制台为您定义的数字提供静态 otp 代码,然后在模拟器上尝试。