gpt4 book ai didi

android - Firebase 电话身份验证流程

转载 作者:行者123 更新时间:2023-11-29 16:33:41 26 4
gpt4 key购买 nike

我对电话验证的正确流程有点困惑。我注意到有几个场景由于无法从 Firebase 中完全删除我的用户并重现所有场景而无法重现。可能的情况是:

  1. 用户从未登录过 firebase
  2. 用户之前登录和注销,并且在登录时收到短信
  3. 用户之前登录和注销,登录时未收到短信

发生在我身上的是场景 2。我的日志是:

D/DTAG: Asking verification for: +972052*****77
D/DTAG: 2. Sending for verification and waiting response to callbacks
D/DTAG: 3.b. Sending code
D/DTAG: 3.a. Verification complete, signing in
D/DTAG: signInWithCredential:success

问题 1:那么现在 SMS 已经无关紧要了,对吧?我可以不检查短信代码登录吗?

问题 2:在场景 1 中,是否根本没有调用回调“onVerificationCompleted”?

问题3:在场景3中,回调“onCodeSent”根本没有被调用?

问题4:如何在“onCodeSent”获取短信验证码?。我知道我可以在“onVerificationCompleted”中使用 phoneAuthCredential.getSmsCode(),但有些情况下它不会被调用。

我的代码:

public class MyVerifyPhoneActivity extends AppCompatActivity {

private static final String TAG = "DTAG";
EditText editTextCode;
Button buttonLogin;
String mVerificationId;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_verify_phone);

editTextCode = findViewById(R.id.editTextCode);
buttonLogin = findViewById(R.id.myButtonSignIn);

String phonenumber = getIntent().getStringExtra("phonenumber");
Log.d(TAG,"Asking verification for: "+phonenumber);
sendVerificationCode(phonenumber);


buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String code = editTextCode.getText().toString().trim();
Log.d(TAG,"Button login clicked");

if (code.isEmpty() || code.length() < 6) {

editTextCode.setError("Enter code...");
editTextCode.requestFocus();
return;
}

verifyCode(code);
}
});


}

private void sendVerificationCode(String number) {

Log.d(TAG,"2. Sending for verification and waiting response to callbacks");
PhoneAuthProvider.getInstance().verifyPhoneNumber(
number,
60,
TimeUnit.SECONDS,
TaskExecutors.MAIN_THREAD,
mCallBack
);

}

private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallBack = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {

Log.d(TAG,"3.a. Verification complete, signing in");
signInWithPhoneAuthCredential(phoneAuthCredential);
}

@Override
public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(verificationId, forceResendingToken);

mVerificationId = verificationId;
Log.d(TAG,"3.b. Sending code");

}

@Override
public void onVerificationFailed(FirebaseException e) {
Log.w(TAG, "onVerificationFailed", e);
Log.d(TAG,"3.c. Failed");

if (e instanceof FirebaseAuthInvalidCredentialsException) {
// Invalid request
// ...
} else if (e instanceof FirebaseTooManyRequestsException) {
// The SMS quota for the project has been exceeded
// ...
}
}
};


private void verifyCode(String code)
{
Log.d(TAG,"verifying Code");
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, code);
signInWithPhoneAuthCredential(credential);
}



private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {

FirebaseAuth.getInstance().signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d("DTAG", "signInWithCredential:success");

FirebaseUser user = task.getResult().getUser();

} else {
// Sign in failed, display a message and update the UI
Log.w("DTAG", "signInWithCredential:failure", task.getException());
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
// The verification code entered was invalid
}
}
}
});
}


}

最佳答案

I cannot reproduce due to reason I can not completely delete my user from Firebase

-> 是的,你可以。看看图片。

Firebase console.

问题 1 的答案:-> 不是。短信显然是相关的。没有短信,电话验证将无法工作。

问题 2 的答案:-> onVerificationCompleted 每次都会被调用。

问题 3 的答案:-> 是的。 onCodeSent 并非每次都被调用。

问题 4 的答案:-> 您不需要在 onCodeSent 方法中使用 OTP。我们可以在onCodeSent中获取verificationIdPhoneAuthProvider.ForceResendingToken。我们需要将verificationId保存在一个变量中。并在身份验证用户输入 OTP 时使用它。

PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, YouEditTextValueString);

我的看法:当我第一次工作时,我的脑海里也有这些问题。我遇到过,如果我们使用具有相同电话号码的相同设备,则不会收到短信,我猜 Firebasegoogle-play-service 会处理它。

不知何故,现在我每次都收到相同设备和电话号码的短信。

但我们不需要关心它。

关于android - Firebase 电话身份验证流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53258790/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com