【问题标题】:Firebase Phone auth is not verifying the OTP enteredFirebase 电话身份验证未验证输入的 OTP
【发布时间】:2019-12-01 02:56:39
【问题描述】:

我正在尝试将电话身份验证系统集成到我的颤振应用程序中。但即使我输入了错误的 OTP,用户也会得到验证并进入下一页。

我正在使用对话框来请求 OTP

      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return new AlertDialog(
          title: Text('Enter sms Code'),
          content: TextFormField(
            controller: _smsController,
            keyboardType: TextInputType.number,
            textInputAction: TextInputAction.done,
            decoration: InputDecoration(
                hintText: 'Enter OTP', icon: Icon(Icons.perm_phone_msg)),
            maxLength: 6,
            maxLengthEnforced: true,
          ),
          contentPadding: EdgeInsets.all(10.0),
          actions: <Widget>[
            new RaisedButton(
                child: Text('Login'),
                textColor: Colors.white,
                onPressed: () {
                  _signInWithPhoneNumber(context);
                })
          ],
        );
      });```

```void _signInWithPhoneNumber(BuildContext context) async {
    final AuthCredential credential = await PhoneAuthProvider.getCredential(
      verificationId: _verificationId,
      smsCode: _smsController.text,
    );
    await _auth
        .signInWithCredential(credential)
        .then((FirebaseUser user) async {
      final FirebaseUser currentUser = await _auth.currentUser();
      assert(user.uid == currentUser.uid);
      Navigator.of(context).pop();
      Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => InfoScreen(_phoneNumberController.text)));
    }).catchError((e) {
      print(e.message);
      Navigator.of(context).pop();
    });
  }
}```

【问题讨论】:

    标签: firebase flutter


    【解决方案1】:

    here 所述,您必须获得相同的身份验证才能验证 Firebase 管理员的 OTP。

    【讨论】:

      【解决方案2】:

      我认为您必须通过将“.then(FirebaseUser ...)”更改为“.addOnCompleteListener(this, new OnCompleteListener(AuthResult res) {});”来添加一个 onCompleteListener;

      AuthResult 对象有一个布尔值“isSuccessful()”,您可以检查它以确保用户输入了正确的代码。

      我假设无论代码是否实际匹配,Firebase 都会返回 FirebaseUser 对象。

      【讨论】:

        【解决方案3】:

        你用PhoneAuthProvider做错了

         final AuthCredential credential = await PhoneAuthProvider.getCredential(
              verificationId: _verificationId,
              smsCode: _smsController.text,
            );
        

        将上面的代码替换为:

         FirebaseAuth.instance
                .signInWithPhoneNumber(verificationId: verificationId, smsCode: smsCode)
                .then((user) {
              Navigator.of(context).pushReplacementNamed('/homepage');
            }).catchError((e) {
              print(e);
            });
        

        使用它,您可以使用代码验证手机。更多详情请访问Here

        【讨论】:

        • 但 FirebaseAuth.instance.signInWithPhoneNumber 已被弃用。
        • @mausomsaikia Check This
        猜你喜欢
        • 2021-05-03
        • 2021-06-27
        • 2021-09-08
        • 2021-02-19
        • 2021-07-13
        • 1970-01-01
        • 2019-08-17
        相关资源
        最近更新 更多