I’m working on Firebase phone authentication and facing an error when trying to create a PhoneAuthCredential
in the verifySignInCode()
method.
This is my code for sending the code:
public void send_code(){
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
Log.d("code", "onCodeSent:" + s);
verificationID=s;
}
@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
}
@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
Toast.makeText(PhoneAuthentication.this, e.getMessage(), Toast.LENGTH_SHORT);
}
};
mPhoneAuthProvider.verifyPhoneNumber(
user_contact , 60, TimeUnit.SECONDS, PhoneAuthentication.this, mCallbacks
);
}
This is my code for verifying the sign in code:
public void verifySignInCode(String code){
try {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationID, code);
signInWithPhoneAuthCredential(credential);
}catch (Exception e){
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
This is my code for signing in with phone credentials:
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential){
mFirebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
startActivity(new Intent(PhoneAuthentication.this, UserHome.class));
}
else {
Log.d("error:", task.getException().getMessage());
}
}
});
}
I’m getting the following error when trying to create a PhoneAuthCredential
in the verifySignInCode()
method:
Cannot create PhoneAuthCredential without either verificationProof, sessionInfo, temporary proof, or enrollment ID.