Я делаю регистрацию пользователя через firebase так же, как регистрация через WhatsApp по номеру телефона, и я хочу, чтобы код подтверждения подтвердил номер телефона пользователя, а затем подтвердил полученный код. Поскольку я получаю код подтверждения, но он успешно авторизован без ввода кода подтверждения и без подтверждения кода подтверждения.
String phoneNumber = InputPhoneNumber.getText().toString();
if (TextUtils.isEmpty(phoneNumber)) {
Toast.makeText(MainActivity.this, "please enter your phone number first...", Toast.LENGTH_SHORT).show();
} else {
loadingBar.setTitle("Phone Verfication");
loadingBar.setMessage("please wait, while we are authenticating your phone...");
loadingBar.setCanceledOnTouchOutside(false);
loadingBar.show();
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
MainActivity.this, // Activity (for callback binding)
callbacks); // OnVerificationStateChangedCallbacks
}
}
});
VerifyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SendVerificationCodeButton.setVisibility(View.INVISIBLE);
InputPhoneNumber.setVisibility(View.INVISIBLE);
String verificationCode = InputVerifcationCode.getText().toString();
if(TextUtils.isEmpty(verificationCode))
{
Toast.makeText(MainActivity.this, "please write verification code first...", Toast.LENGTH_SHORT).show();
}
else
{
loadingBar.setTitle("Verfication Code");
loadingBar.setMessage("please wait, while we are verifying verification code...");
loadingBar.setCanceledOnTouchOutside(false);
loadingBar.show();
PhoneAuthCredential credential = PhoneAuthProvider.getCredential( mVerificationId, verificationCode);
signInWithPhoneAuthCredential(credential);
}
}
});
callbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
signInWithPhoneAuthCredential(phoneAuthCredential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
loadingBar.dismiss();
Toast.makeText(MainActivity.this, "Invalid phone number please enter correct number with your country code...", Toast.LENGTH_SHORT).show();
SendVerificationCodeButton.setVisibility(View.VISIBLE);
InputPhoneNumber.setVisibility(View.VISIBLE);
VerifyButton.setVisibility(View.INVISIBLE);
InputVerifcationCode.setVisibility(View.INVISIBLE);
}
@Override
public void onCodeSent(String verificationId,
PhoneAuthProvider.ForceResendingToken token) {
mVerificationId = verificationId;
mResendToken = token;
loadingBar.dismiss();
Toast.makeText(MainActivity.this, "code has been sent, please check and verify", Toast.LENGTH_SHORT).show();
SendVerificationCodeButton.setVisibility(View.INVISIBLE);
InputPhoneNumber.setVisibility(View.INVISIBLE);
VerifyButton.setVisibility(View.VISIBLE);
InputVerifcationCode.setVisibility(View.VISIBLE);
}
};
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
String currentUserId = mAuth.getCurrentUser().getUid();
RootRef.child("User").child(currentUserId).setValue("");
loadingBar.dismiss();
Toast.makeText(MainActivity.this, "Congratulations, you are logged in successfully", Toast.LENGTH_SHORT).show();
} else {
String message = task.getException().toString();
Toast.makeText(MainActivity.this, "Error: " + message, Toast.LENGTH_SHORT).show();
}
}
});
}
}