Phone Number Authentication in Android Using Firebase Auth

Code Gradients
3 min readJan 30, 2020

--

Phone number authentication has become popular in the last few years.

Validating identity is incredibly important in mobile development, and assuring new user signups are real human beings is critical. Developers need reliable ways to confirm the identities of their users to prevent security issues.

Well, adding phone number authentication in your app has never been so easier.

Start By Connecting your project to firebase.

Developers can implement verification systems using phone numbers in two main ways: either by calling or by sending an SMS containing a code that the user must input.

Enable Phone Number sign-in for your Firebase project

To be able to authenticate the user by SMS, you must first enable Phone Number sign-in method in your Firebase console and click save

To initialize the Sign-in process, present the user a screen to input his/her phone number. Also note: Alway let the user know they might receive an SMS message for verification and standard rates apply.

Then, pass their phone number to the PhoneAuthProvider.verifyPhoneNumber method to request that Firebase verify the user's phone number. For example,

PhoneAuthProvider.OnVerificationStateChangedCallbacks callbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
//Detect the SMS Code
String code = phoneAuthCredential.getSmsCode();

if (code != null) {
try {
//Verify the Code
verifyVerificationCode(code);
} catch (Exception e) {
e.printStackTrace();
}
}
}

@Override
public void onVerificationFailed(FirebaseException e) {
Toast.makeText(OTPVerificationActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
Log.v("Login__", "Error: exception: " + e);
}

@Override
public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
OTPVerificationActivity.this.verificationId = verificationId;
}
};

try {
PhoneAuthProvider.getInstance().verifyPhoneNumber(s, 60, TimeUnit.SECONDS, this, callbacks);
} catch (Exception e) {
e.printStackTrace();
}

Then, this method will be called when you will receive the verification code

You can also call the method below by taking input from the user and call the function.

public void verifyVerificationCode(String otp) {
try {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, otp);
//Sign In The User
signInWithPhoneAuthCredentials(credential);
} catch (Exception e) {
e.printStackTrace();
}
}

Here comes the main method in which we login into the firebase

public void signInWithPhoneAuthCredentials(PhoneAuthCredential credential) {
auth.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
//The task is successfull. You are now signed in
//navigate to the main activity after signing IN.

} else {
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
String message = "Invalid Code Entered";
avi.setVisibility(View.GONE);
continueBtn.setEnabled(true);
Toast.makeText(OTPVerificationActivity.this, message, Toast.LENGTH_SHORT).show();
}
}

}
});

So you are logged in now.

Extra Note

Well, for starters, you will have to add test numbers in the phone auth in firebase auth settings. Like,

But when you release your app to the play store, the firebase will send real authentication codes to your app. To do this, you will have to do this

Firstly, remove all the test numbers from here(shown above)

After that get the AUTH key from the play store app(That you have uploaded just now) and paste it in the Firebase Project Settings in SHA certificate fingerprints as shown below

That’s all. You have successfully implemented Phone Number Authentication in your android App. Congratulations!

--

--

Code Gradients

Code Gradients is a one stop shop for bringing your app idea to life. It is our mission to provide affordable, & reliable app development for everyone!