Firebase 电话身份验证在 APK 发布模式下不起作用

Posted

技术标签:

【中文标题】Firebase 电话身份验证在 APK 发布模式下不起作用【英文标题】:Firebase Phone Auth not working in APK Release mode 【发布时间】:2020-10-04 15:19:26 【问题描述】:

我阅读了以下两个问题,但这些都不是我的问题的解决方案:

    Firebase Phone Auth not working in release build

    Firebase Authentication not working in Signed APK

这两个问题中提到的解决方案都是说我需要从 Play 控制台复制 SHA-1 并将其放置在我的应用的 Firebase 控制台 SHA-1 中。问题是My app is not on play store, I just signed it and build the apks,就像我们通常对 apk.relase 所做的那样。我还更新了 firebase_authfirebase_core 依赖项到最新版本。

下面是我的手机验证码:


  verificationCompleted() 
    return (AuthCredential credential) async 
      await _auth.signInWithCredential(credential).then((AuthResult value) 
        if (value.user != null) 
          Navigator.pop(context);
          Navigator.push(
              context,
              new MaterialPageRoute(
                  builder: (context) => AddContacts(
                        user: value.user,
                      )));
        
      );
    ;
  

  verificationFailed() 
    return (AuthException exception) 
      Toast.show('Verification Failed: $exception.code', context,
          backgroundColor: Colors.red,
          duration: 3,
          backgroundRadius: 5,
          gravity: Toast.BOTTOM);
    ;
  

  codeSent() 
    return (String verificationID, [int forceResendingToken]) 
      showDialog(
          context: context,
          barrierDismissible: false,
          builder: (context) 
            return AlertDialog(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(12)),
              title: Text('Enter 6-Digit Code'),
              content: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  TextField(
                    style: TextStyle(
                        fontSize: MediaQuery.of(context).size.height * 0.025),
                    controller: _controllerCode,
                    keyboardType: TextInputType.number,
                    maxLength: 6,
                    cursorColor: Colors.black,
                    decoration: InputDecoration(
                      errorText: validateCode(_controllerCode.text),
                      hintText: 'Enter Code',
                      enabledBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(12.0)),
                      focusedBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(12.0)),
                      errorBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(12.0),
                          borderSide: BorderSide(color: Colors.red)),
                      focusedErrorBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(12.0),
                          borderSide: BorderSide(color: Colors.red)),
                    ),
                  ),
                  SizedBox(
                    height: MediaQuery.of(context).size.height * 0.01,
                  ),
                  Row(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Icon(
                        Icons.info,
                        color: Colors.red,
                        size: MediaQuery.of(context).size.height * 0.03,
                      ),
                      SizedBox(
                        width: MediaQuery.of(context).size.width * 0.02,
                      ),
                      Text(
                        'Wait for Automatic Detection!',
                        style: TextStyle(
                            fontFamily: 'Sogoe',
                            color: Colors.red,
                            fontSize:
                                MediaQuery.of(context).size.height * 0.017),
                      ),
                    ],
                  ),
                  SizedBox(
                    height: MediaQuery.of(context).size.height * 0.01,
                  ),
                  Container(
                    height: MediaQuery.of(context).size.height * 0.06,
                    width: MediaQuery.of(context).size.width,
                    child: FlatButton(
                      padding: EdgeInsets.symmetric(
                          horizontal: MediaQuery.of(context).size.width * .05),
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(5)),
                      child: Text(
                        "Confirm",
                      ),
                      textColor: Colors.white,
                      color: Colors.lightBlue,
                      onPressed: () async 
                        setState(() 
                          _controllerCode.text.isEmpty
                              ? codeValidate = true
                              : codeValidate = false;
                        );
                        _auth.currentUser().then((user) 
                          if (user != null) 
                            Navigator.pop(context);
                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => AddContacts(
                                          user: user,
                                        )));
                           else 
                            _verifyCode();
                          
                        );
                        _controllerCode.clear();
                      ,
                    ),
                  )
                ],
              ),
            );
          );
      _verificationId = verificationID;
    ;
  

  codeAutoRetrievalTimeout() 
    return (String verificationID) 
      _verificationId = verificationID;
    ;
  

  Future<void> _verifyPhoneNumber(String phone) async 
    _auth.verifyPhoneNumber(
      phoneNumber: phone,
      timeout: const Duration(seconds: 60),
      verificationCompleted: verificationCompleted(),
      verificationFailed: verificationFailed(),
      codeSent: codeSent(),
      codeAutoRetrievalTimeout: codeAutoRetrievalTimeout(),
    );
  

  Future<void> _verifyCode() async 
    final AuthCredential credential = PhoneAuthProvider.getCredential(
        verificationId: _verificationId, smsCode: _controllerCode.text);
    final FirebaseUser user =
        (await _auth.signInWithCredential(credential)).user;
    final FirebaseUser currentUser = await _auth.currentUser();
    assert(user.uid == currentUser.uid);
    if (user != null) 
      Navigator.pop(context);
      Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => AddContacts(
                    user: user,
                  )));
    
  

【问题讨论】:

您是否将发布 apk 的 SHA 添加到 firebase? @JideGuru 是的,我做到了,解决方案发布在下面 SHA-1 对于调试和发布模式是分开的:) 【参考方案1】:

找到解决办法

是的,我添加了 SHA-1 密钥,但我知道用于调试和发布模式的 SHA-1 是分开的,这 answer is solution

【讨论】:

【参考方案2】:

将您的 PLAYSTORE 签名密钥证书添加到 Firebase SHA1 和 SHA256 证书。 您将在

中找到密钥证书

googele play console > 发布下 - 设置 > 应用完整性 > 应用签名密钥证书

【讨论】:

以上是关于Firebase 电话身份验证在 APK 发布模式下不起作用的主要内容,如果未能解决你的问题,请参考以下文章

firebase电话身份验证在发行版中不起作用[重复]

Flutter bloc 和 Firebase 电话身份验证

使用 BLoC 进行 Flutter Firebase 电话身份验证

Firebase 身份验证在发布模式下失败

如何将 Flutter Bloc 与 Firebase 电话身份验证一起使用

使用firebase电话身份验证验证失败