Firebase Phone Verified

Viewed 728

Is there method in Firebase I can call to check if user has verified phone number? I already have everything working out properly (user enters country code/phone number, automatically gets entered OTP code and starting proper activity), but I would like to check if user has verified phone number, in case he doesn't have I want to forbid him from accessing certain activity.

For email, there is isEmailVerified() but for phone verification there is only getPhoneNumber method.

4 Answers

If everything is working out properly, That means the user has a verified phone, it's should be an axiom :"If the user pass the registration phone activity that means his phone got verified so you don't need to check this again.".

If you really have to check if the phone is verified, You have to done this manually, If you are using only Firebase as as backend you need to save each phone if verified (ex : "+2136xxxxxx":"true").

If you are using another backend SQL you can make a table user with column phoneIsVerified and update it when required.

You can check if userId or phone number is null by -

FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

if(firebaseUser.getUid() == null || firebaseUser.getPhoneNumber() == null){
    // User is not logged in
}

Edited

If you want your user to login again with phone verification you can sign out your user with -

if(firebaseUser.getUid() != null){
     // User is logged in
     FirebaseAuth.getInstance().signOut();
}

I'm not sure about the flow of your app but from what I understand: Users can use your app without being authenticated. However, you want unauthenticated users to have access to only a limited number of features.

You mentioned in the comments that users are required to verify their phone number on every fresh install of the app regardless of previous use. Therefore, you can use SharedPreferences to your advantage like this:

  • Every time the user verifies their phone number, save a boolean value (true) to the SharedPreferences.
  • When a user wants to access a feature that is only available to authenticated users, check the saved SharedPreferences boolean value to check whether the user is authenticated or not.
  • When a user signs out, remember to delete/reset the SharedPreferences boolean value.

To save a boolean value to SharedPreferences, you can do something like this:
// create a SharedPreferences object and reference it with "AUTH_STATUS"
SharedPreferences sharedPreferences = getSharedPreferences("AUTH_STATUS", Context.MODE_PRIVATE);

// create an editor object from your sharedpreferences object
SharedPreferences.Editor editor = sharedPreferences.edit();

// save your boolean value to a reference called "status"
editor.putBoolean("status", true);

// save changes
editor.apply();

To retrieve a saved boolean value, you can do something like this:

// create a SharedPreferences object and reference it with "AUTH_STATUS"
SharedPreferences sharedPreferences = getSharedPreferences("AUTH_STATUS", Context.MODE_PRIVATE);

// get saved boolean value, default value should be false
boolean ab  = sharedPreferences.getBoolean("status", false);

To delete/reset the SharedPreferences boolean value, simply set the boolean value to false as shown below:

// create a SharedPreferences object and reference it with "AUTH_STATUS"
SharedPreferences sharedPreferences = getSharedPreferences("AUTH_STATUS", Context.MODE_PRIVATE);

// create an editor object from your sharedpreferences object
SharedPreferences.Editor editor = sharedPreferences.edit();

// save your boolean value to a reference called "status"
editor.putBoolean("status", false);

// save changes
editor.apply();

According to the codes that I employ from my tutorial video , user.getPhoneNumber() will not contain any numbers if it isn't verified. It will not even show up on the Authentication list at the Firebase website to begin with.

Given this knowledge, I check not only for user.getPhoneNumber() not being null, I also check for user.getPhoneNumber() not containing "+" (because if it contains "+" means there is a verified phone number).

The following codes help solved the 'email is verified?' and 'phone number is verified?' conundrum.

FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();

if (user != null &&
(user.getPhoneNumber() == null ||
(user.getPhoneNumber() != null && 
!user.getPhoneNumber().contains("+")))
) {
   if (!user.isEmailVerified()) {
   verifyMsg.setVisibility(View.VISIBLE);
   editor.putString("capturegoogleloginonetime", "1");
   editor.apply();
   resendCode.setVisibility(View.VISIBLE);

   resendCode.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
             //send verification link
             FirebaseUser fuser = mAuth.getCurrentUser();
                                
             user.sendEmailVerification().addOnSuccessListener(new 
             OnSuccessListener<Void>() {
                  @Override
                  public void onSuccess(Void aVoid) {
                  Toast.makeText(v.getContext(), "Verification Email has been Sent.",
                  Toast.LENGTH_SHORT).show();
                  }

             }).addOnFailureListener(new OnFailureListener() {
                  @Override
                  public void onFailure(@NonNull Exception e) {
                  Toast.makeText(LoginActivity.this, 
                  "Error: Email not sent " + e.getMessage(),
                  Toast.LENGTH_SHORT).show();
                  }
                });
               }
             });
            }
           }
    
if (user != null && user.getPhoneNumber() != null 
&& user.getPhoneNumber().contains("+")) {
   SendUserToMainActivity();
}

if (user != null && user.isEmailVerified() ) {
   SendUserToMainActivity();
}
Related