now auth phone is available in firebase. Here is the code for Auth phone using Firebase: If you have any questions, ask me for free.
EditText phoneNum,Code; //// two edit text one for enter phone number other for enter OTP code Button sent_,Verify; // sent_ button to request for verification and verify is for to verify code private PhoneAuthProvider.ForceResendingToken mResendToken; private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks; private FirebaseAuth mAuth; private String mVerificationId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_phone_number_auth); phoneNum =(EditText) findViewById(R.id.fn_num); Code =(EditText) findViewById(R.id.code); sent_ =(Button)findViewById(R.id.sent_nu); Verify =(Button)findViewById(R.id.verify); callback_verificvation(); mAuth = FirebaseAuth.getInstance(); sent_.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String num=phoneNum.getText().toString(); startPhoneNumberVerification(num); // call function for receive OTP 6 digit code } }); Verify.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String code=Code.getText().toString(); verifyPhoneNumberWithCode(mVerificationId,code); //call function for verify code } }); } private void startPhoneNumberVerification(String phoneNumber) { // [START start_phone_auth] PhoneAuthProvider.getInstance().verifyPhoneNumber( phoneNumber, // Phone number to verify 60, // Timeout duration TimeUnit.SECONDS, // Unit of timeout this, // Activity (for callback binding) mCallbacks); // OnVerificationStateChangedCallbacks // [END start_phone_auth] } 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 information FirebaseUser user = task.getResult().getUser(); Toast.makeText(getApplicationContext(), "sign in successfull", Toast.LENGTH_SHORT).show(); // [START_EXCLUDE] // [END_EXCLUDE] } else { // Sign in failed, display a message and update the UI if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) { // The verification code entered was invalid // [START_EXCLUDE silent] // [END_EXCLUDE] } // [START_EXCLUDE silent] // Update UI // [END_EXCLUDE] } } }); } private void verifyPhoneNumberWithCode(String verificationId, String code) { // [START verify_with_code] PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code); // [END verify_with_code] signInWithPhoneAuthCredential(credential); } private void callback_verificvation() { mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { @Override public void onVerificationCompleted(PhoneAuthCredential credential) { signInWithPhoneAuthCredential(credential); } @Override public void onVerificationFailed(FirebaseException e) { // This callback is invoked in an invalid request for verification is made, } @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. // Save verification ID and resending token so we can use them later mVerificationId = verificationId; mResendToken = token; } };
source share