【问题标题】:How to make sign out from firebase如何从firebase注销
【发布时间】:2017-12-31 15:31:50
【问题描述】:

这是我用于 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();
         }
        }
     }
 });
}

【问题讨论】:

  • otp ?那是什么意思?
  • 发布您的登录代码,因为对我来说您的注销很好,但您再次登录时遇到问题,谢谢:)
  • 其实这个方法是在验证移动OTP代码时运行的。
  • 等待我上传。
  • (第一个代码块后面跟着一个悬空的“}”:修正缩进。)

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


【解决方案1】:

我真正想用awaik's 回答的一件事是,当您的应用程序处于开发阶段时,谷歌建议不要使用实数接收短信,这会影响您的配额并导致您受到一些限制。谷歌提供使用白名单号码当您的应用程序处于开发阶段时。什么白名单号码可以帮助您以模拟方式完成验证步骤。要创建白名单号码,请转到您的 Firebase 控制台->转到身份验证->选择登录方法->电话登录。从那里您可以创建一个白名单号码或使用现有的号码(白名单号码必须是在现实世界中通常不使用的号码),您还可以选择设置一个 6 位数的虚拟 OTP。我添加了一张图片这可能对你有帮助

【讨论】:

    【解决方案2】:

    Firebase 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.');
          };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-23
      • 2017-10-10
      • 2021-07-30
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 2018-11-30
      • 2022-01-04
      相关资源
      最近更新 更多