如何从firebase签出

时间:2017-07-25 15:35:41

标签: android firebase firebase-authentication one-time-password

这是我用于firebase登录的代码。完成验证后,我正在退出。但是,当我尝试使用相同的号码再次登录时,它不会发送给我otp。

帮我注册一下。 提前致谢。 这是代码:

 @Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
 // This callback will be invoked in two situations:
 // 1 - Instant verification. In some cases the phone number can be instantly
 //     verified without needing to send or enter a verification code.
      // 2 - Auto-retrieval. On some devices Google Play services can automatically
 //     detect the incoming verification SMS and perform verificaiton without
 //     user action.
// Log.d(TAG, "onVerificationCompleted:" + credential);
    //mAuth.signOut();
     mAuth= FirebaseAuth.getInstance();
     if(mAuth!=null)
         mAuth.signOut();
     Toast.makeText(MainActivity.this,"verification complete",Toast.LENGTH_SHORT).show();

}

 @Override
 public void onCodeSent(String verificationId,
    PhoneAuthProvider.ForceResendingToken token) {
 // The SMS verification code has been sent to the provided phone number, we
 // now need to ask the user to enter the code and then construct a credential
 // by combining the code with a verification ID.
//   Log.d(TAG, "onCodeSent:" + verificationId);
     Toast.makeText(MainActivity.this,"code sent",Toast.LENGTH_SHORT).show();
 // Save verification ID and resending token so we can use them later
 mVerificationId = verificationId;
 mResendToken = token;
mobileNumber.setVisibility(View.GONE);
     submit.setVisibility(View.GONE);
    otpButton.setVisibility(View.VISIBLE);
     otpText.setVisibility(View.VISIBLE);
     t1.setVisibility(View.GONE);
     t2.setVisibility(View.VISIBLE);
     mAuth= FirebaseAuth.getInstance();
 }

 otpButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId,otpText.getText().toString());
            signInWithPhoneAuthCredential(credential);
        }
    });

    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PhoneAuthProvider.getInstance().verifyPhoneNumber(
                    "+91"+mobileNumber.getText().toString(),// Phone number to verify
                    60,// Timeout duration
                    TimeUnit.SECONDS,// Unit of timeout
                    MainActivity.this,// Activity (for callback binding)
            mCallbacks);
        }
    });
}

private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
 mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
     @Override
     public void onComplete(@NonNull Task<AuthResult> task) {
         if (task.isSuccessful()) {
             Toast.makeText(MainActivity.this,"verification 
                 done",Toast.LENGTH_SHORT).show();
            FirebaseUser user = task.getResult().getUser();
        } else {
        if (task.getException() instanceof 
            FirebaseAuthInvalidCredentialsException) {
               Toast.makeText(MainActivity.this,"verification failed code 
                invalid",Toast.LENGTH_SHORT).show();
         }
        }
     }
 });
}

2 个答案:

答案 0 :(得分:0)

Firebase SMS授权使用具有立即验证的电话号码的逻辑。

这意味着当您在真实设备上运行应用并要求发送SMS时:

  • 第一次收到短信。
  • 注销后
  • 然后,您再次请求SMS,然后...您将不会收到它。您将收到“ verificationCompleted”回调,因为框架可以识别相同的SIM卡和设备,并且不会再次请求SMS。

让我们看一下方法

  /// [verificationCompleted] This callback must be implemented.
  ///   It will trigger when an SMS is auto-retrieved or the phone number has
  ///   been instantly verified. The callback will receive an [AuthCredential]
  ///   that can be passed to [signInWithCredential] or [linkWithCredential].

  /// [codeSent] Optional callback.
  ///   It will trigger when an SMS has been sent to the users phone,
  ///   and will include a [verificationId] and [forceResendingToken].
  ///
  /// [codeAutoRetrievalTimeout] Optional callback.
  ///   It will trigger when SMS auto-retrieval times out and provide a
  ///   [verificationId].
  Future<void> verifyPhoneNumber({
    @required String phoneNumber,
    @required Duration timeout,
    int forceResendingToken,
    @required PhoneVerificationCompleted verificationCompleted,
    @required PhoneVerificationFailed verificationFailed,
    @required PhoneCodeSent codeSent,
    @required PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout,
  }

您会看到有“ [verificationCompleted]此回调必须实现。”。

因此,只需将此逻辑添加到您的代码中即可。例如:

final PhoneVerificationCompleted verificationCompleted =
          (AuthCredential phoneAuthCredential) async {
        user = (await _auth.signInWithCredential(phoneAuthCredential)).user;
        print('Phone number already checked.');
      };

答案 1 :(得分:0)

我真的想添加awaik's答案的一件事是,当您的应用处于开发阶段时,谷歌建议不要使用实数来获取短信,这会影响您的配额并给您带来一些限制。当应用处于开发阶段时,白名单编号。什么白名单编号可以让您方便地以模拟方式完成验证步骤。要创建白名单编号,请转到Firebase控制台->进行身份验证->选择登录方法- >电话登录。从那里您可以创建白名单号码或使用现有的号码(白名单号码必须是现实世界中通常不使用的号码),还可以选择设置6位数字的虚拟OTP。可能对您有帮助的图片enter image description here