Reputation: 45
I created a connection by phone number, all works very well. My only problem is that the number does not appear in the IDs on firebase.
With google auth it works very well but no with phone auth. I probably forgot something, but I can't find it.
Do you have an idea?
My activity auth phone:
public class Activity_aunth_phone extends AppCompatActivity {
EditText numero;
String verificationId;
FirebaseAuth mAuth;
Button button;
EditText cp;
private static final String TAG = "PhoneAuthActivity";
private static final String KEY_VERIFY_IN_PROGRESS = "key_verify_in_progress";
private static final int STATE_INTIALIZED = 1;
private static final int STATE_CODE_SEND = 2;
private static final int STATE_VERIFY_FAILED = 3;
private static final int STATE_VERIFY_SUCCES = 4;
private static final int STATE_SIGNIN_FAILED = 5;
private static final int STATE_SIGNIN_SUCCES = 6;
private FirebaseAuth auth;
private boolean mVerificationInProgress = false;
private String mVerificationId;
private PhoneAuthProvider.ForceResendingToken mResendToken;
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aunth_phone);
cp = findViewById(R.id.cp);
numero = (EditText) findViewById(R.id.num);
button = findViewById(R.id.button);
auth = FirebaseAuth.getInstance();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
attemptLogin();
}
});
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
// This callback will be invoked in two situations:
// 1 - Instant verification. In some cases the phone number can be instantly
// verified without needing to send or enter a verification code.
// 2 - Auto-retrieval. On some devices Google Play services can automatically
// detect the incoming verification SMS and perform verification without
// user action.
Log.d(TAG, "onVerificationCompleted:" + credential);
signInWithPhoneAuthCredential(credential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
// This callback is invoked in an invalid request for verification is made,
// for instance if the the phone number format is not valid.
Log.w(TAG, "onVerificationFailed", e);
if (e instanceof FirebaseAuthInvalidCredentialsException) {
// Invalid request
// ...
} else if (e instanceof FirebaseTooManyRequestsException) {
// The SMS quota for the project has been exceeded
// ...
}
// Show a message and update the UI
// ...
}
@Override
public void onCodeSent(String verificationId,
PhoneAuthProvider.ForceResendingToken token) {
// The SMS verification code has been sent to the provided phone number, we
// now need to ask the user to enter the code and then construct a credential
// by combining the code with a verification ID.
Log.d(TAG, "onCodeSent:" + verificationId);
// Save verification ID and resending token so we can use them later
mVerificationId = verificationId;
mResendToken = token;
// ...
}
};
}
private void attemptLogin() {
numero.setError(null);
String cpp = cp.getText().toString();
String number = numero.getText().toString();
String phone = cpp + number;
startPhoneNumberVerification(phone);
boolean cancel = false;
View focusView = null;
if (!isPhoneValid(phone)) {
focusView = numero;
cancel = true;
}
if (cancel) {
focusView.requestFocus();
} else {
startPhoneNumberVerification(phone);
}
}
private boolean isPhoneValid(String phone){
return phone.length() > 8;
}
private void startPhoneNumberVerification(String phoneNumber) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber,
60,
TimeUnit.SECONDS,
this,
mCallbacks);
mVerificationInProgress = true;
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.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(TAG, "signInWithCredential:success");
Intent intent = new Intent(Activity_aunth_phone.this, Activity_profile.class);
startActivity(intent);
FirebaseUser user = task.getResult().getUser();
// ...
} else {
// Sign in failed, display a message and update the UI
Log.w(TAG, "signInWithCredential:failure", task.getException());
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
// The verification code entered was invalid
}
}
}
});
}
}
Upvotes: 0
Views: 51
Reputation: 6919
You forgot to put the Code :
private void verifyPhoneNumberWithCode(String verificationId, String code) {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, code); // Just Pass the otp in Code Section.
signInWithPhoneAuthCredential(credential);
}
Which provide credentials to new User.
Please Check this link : https://firebase.google.com/docs/auth/android/phone-auth?authuser=1#create-a-phoneauthcredential-object
Upvotes: 2