Firebase电话身份验证未验证输入的OTP

时间:2019-07-23 06:17:48

标签: firebase flutter

我正在尝试在我的flutter应用程序中集成电话身份验证系统。但是,即使我输入了错误的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();
    });
  }
}```

3 个答案:

答案 0 :(得分:0)

您在PhoneAuthProvider上犯了一个错误

您需要使用:

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

使用此功能,您可以使用代码验证电话。有关更多详细信息,请访问Here

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

您必须获得相同的auth身份,才能验证here中所述的firebase admin的OTP。