短信代码已过期。请重新发送验证码,然后重试。扑

时间:2020-06-12 16:43:49

标签: flutter firebase-authentication

我正在尝试使用Phone Auth在Flutter中使用Firebase进行登录,我用此代码绕了几天,我正在使用Provide软件包处理它。我在这里留下了一部分代码,如果您可以帮助我找到错误,我将不胜感激。尝试登录时,显示以下错误。短信代码已过期。请重新发送验证码,然后重试。,否,我仍然不完全了解Firebase如何完全与Flutter配合使用,对此我有点陌生,如果有人可以帮助我查看此代码,错误,或者仅仅是包装失败

_startAuth()async{
    final PhoneCodeSent codeSent =
        (String verificationId, [int forceResendingToken]) async {
      actualCode = verificationId;
      _addStatusMessage("\nEnter the code sent to " + phone);
      _addStatus(PhoneAuthState.CodeSent);
      // if (onCodeSent != null) onCodeSent();
    };

    final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
        (String verificationId) {
    };

    final PhoneVerificationFailed verificationFailed =
        (AuthException authException) {
      _addStatusMessage('${authException.message}');
      _addStatus(PhoneAuthState.Failed);
      if (onFailed != null) onFailed();
      if (authException.message.contains('not authorized'))
        _addStatusMessage('App not authroized');
      else if (authException.message.contains('Network'))
        _addStatusMessage(
            'Please check your internet connection and try again');
      else
        _addStatusMessage('Something has gone wrong, please try later ' +
            authException.message);

    };
    final PhoneVerificationCompleted verificationCompleted =
        (AuthCredential auth) async {
    };

    _addStatusMessage('Phone auth started');
    await FirebaseAuth.instance
        .verifyPhoneNumber(
            phoneNumber: phone.toString(),
            timeout: Duration(seconds: 60),
            verificationCompleted: verificationCompleted,
            verificationFailed: verificationFailed,
            codeSent: codeSent,
            codeAutoRetrievalTimeout: codeAutoRetrievalTimeout)
        .then((value) {
      if (onCodeSent != null) onCodeSent();
      _addStatus(PhoneAuthState.CodeSent);
      _addStatusMessage('Code sent');
    }).catchError((error) {
      if (onError != null) onError();
      _addStatus(PhoneAuthState.Error);
      _addStatusMessage(error.toString());
    });
  }


  void verifyOTPAndLogin({String smsCode}) async {
    smscode = smsCode;  
    final FirebaseAuth _auth =  FirebaseAuth.instance;
    _authCredential = PhoneAuthProvider.getCredential(
        verificationId: actualCode, smsCode: smsCode);

    AuthResult result = await _auth.signInWithCredential(_authCredential);
    FirebaseUser user = result.user;
    PhoneUser.user = user;
    final DocumentSnapshot result1 = await Firestore.instance
      .collection('users')
      .document(user.uid)
      .collection(user.uid)
      .document(user.uid)
      .get();
    if (result1.exists) {
    } else {
      await UsuarioProvider(uid: user.uid).createUserData(
        false,
        user.displayName ?? '',
        user.email ?? '',
        'preferences',
        '',
        user.uid,
        user.photoUrl ?? '',
        false,
        false
      );
    }
    FirebaseAuth.instance
      .signInWithCredential(_authCredential)
      .then((AuthResult result) async {
      _addStatusMessage('Authentication successful');
      _addStatus(PhoneAuthState.Verified);
      if (onVerified != null) onVerified();
    }).catchError((error) {
      if (onError != null) onError();
      _addStatus(PhoneAuthState.Error);
      _addStatusMessage(
          'Something has gone wrong, please try later(signInWithPhoneNumber) $error');
    });


  }

  Future <FirebaseUser> login ()async{
    final FirebaseAuth _auth =  FirebaseAuth.instance;
    _authCredential = PhoneAuthProvider.getCredential(
    verificationId: actualCode, smsCode: smscode);

    AuthResult result = await _auth.signInWithCredential(_authCredential);
    FirebaseUser user = result.user;
    // PhoneUser.user = user;
    return user;
  }

1 个答案:

答案 0 :(得分:0)

使用短信代码的包裹本身完全有道理,一旦使用短信代码,它就会过期。 因此,在 FirebaseAuth.instance.verifyPhoneNumber 方法下的设置中,将verificationCompleted 设置为 null

以便您可以使用您的代码:

final FirebaseAuth _auth =  FirebaseAuth.instance;
_authCredential = PhoneAuthProvider.getCredential(
    verificationId: verificationId, 
    smsCode: smsCode, 
);

验证短信代码