【发布时间】:2019-04-14 23:23:57
【问题描述】:
我对电话身份验证的正确流程有点困惑。 我注意到有几个场景我无法重现,因为我无法从 Firebase 中完全删除我的用户并重现所有场景。可能的情况是:
- 用户从未登录到 Firebase
- 用户之前登录并退出,登录时会收到短信
- 用户之前登录并退出,登录时未收到短信
发生在我身上的是场景 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
}
}
}
});
}
}
【问题讨论】:
-
验证码仅在您验证的号码不在同一部手机中时发送。
-
这对我来说不是这样,在使用手机 B 和手机 A 号码进行一些测试后,现在我每次在手机 A 和使用手机 A 时都会收到代码。
标签: android firebase firebase-authentication