in auth/app/src/main/java/com/google/firebase/quickstart/auth/java/MultiFactorEnrollFragment.java [70:130]
private void onClickVerifyPhoneNumber() {
String phoneNumber = mBinding.fieldPhoneNumber.getText().toString();
OnVerificationStateChangedCallbacks callbacks =
new OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
// Instant-validation has been disabled (see requireSmsValidation below).
// Auto-retrieval has also been disabled (timeout is set to 0).
// This should never be triggered.
throw new RuntimeException(
"onVerificationCompleted() triggered with instant-validation and auto-retrieval disabled.");
}
@Override
public void onCodeSent(
final String verificationId, PhoneAuthProvider.ForceResendingToken token) {
Log.d(TAG, "onCodeSent:" + verificationId);
Toast.makeText( getContext(), "SMS code has been sent", Toast.LENGTH_SHORT)
.show();
mCodeVerificationId = verificationId;
}
@Override
public void onVerificationFailed(FirebaseException e) {
Log.w(TAG, "onVerificationFailed ", e);
Toast.makeText(getContext(), "Verification failed: " + e.getMessage(), Toast.LENGTH_SHORT)
.show();
}
};
FirebaseAuth.getInstance()
.getCurrentUser()
.getMultiFactor()
.getSession()
.addOnCompleteListener(
new OnCompleteListener<MultiFactorSession>() {
@Override
public void onComplete(@NonNull Task<MultiFactorSession> task) {
if (task.isSuccessful()) {
PhoneAuthOptions phoneAuthOptions =
PhoneAuthOptions.newBuilder()
.setPhoneNumber(phoneNumber)
// A timeout of 0 disables SMS-auto-retrieval.
.setTimeout(0L, TimeUnit.SECONDS)
.setMultiFactorSession(task.getResult())
.setCallbacks(callbacks)
// Disable instant-validation.
.requireSmsValidation(true)
.build();
PhoneAuthProvider.verifyPhoneNumber(phoneAuthOptions);
} else {
Toast.makeText(getContext(),
"Failed to get session: " + task.getException(), Toast.LENGTH_SHORT)
.show();
}
}
});
}