Implement Password Authentication in Android Applications

554
SHARES
2.5k
VIEWS

In this lecture, we will learn how we can implement password authentication in android applications. The overall process of implementing authentication should be really straight forward. So without further ado, let’s start

To make our life easier, we’re going to use the awesome Firebase Auth UI. Using the Firebase Auth UI will save a lot of hassle and time when it comes to building the actual user interface and handling the different intent calls between the application activities. Let’s see how we can do that.

Let’s Start

But first, let’s start configuring our android application and downloading all the necessary dependencies. Open your build.gradle file and add the following lines and run gradle sync:

implements 'com.firebaseuifirebase-ui-auth:19.2.0'

Let’s imagine that we are going to add the authentication functionality in the MainActivity file. Open MainActivity.java file and add the following line:

FirebaseAuth auth; 
private static final int RC_SIGN_IN = 17;

Then Inside the onCreate() method, add the following lines of code:

 auth = FirebaseAuth.getInstance();
      if(auth.getCurrentUser() != null) {
          Log.d("Auth", "Logged in successfully");
      } else {
          startActivityForResult(
            AuthUI.getInstance()
             .createSignInIntentBuilder()
              .setAvailableProviders(
               Arrays.asList(new   
              AuthUI.IdpConfig.Builder(                                                         
         AuthUI.EMAIL_PROVIDER).build())).build(),            
         RC_SIGN_IN);findViewById(R.id.logoutBtn)
        .setOnClickListener(this);

Now, in our activity, we need to implement the View.OnClick listener:

 public class MainActivity extends AppCompatActivity   
     implements View.OnClickListener {}

And then we have to implement the onClick function:

    @Override
        public void onClick(View v) {
          if(v.getId() == R.id.logoutBtn) {     
        AuthUI.getInstance().signOut(this)
        .addOnCompleteListener(
        new OnCompleteListener() {                         
         @Override
         public void onComplete(@NonNull Task task)    
            {
             Log.d("Auth", "User Logged out successfully");
             // TODO: make custom operation.
             }
           });
         }
      }

At the end, implement the onActivityResult method as shown in the following code block:

     @Override
         protected void onActivityResult(int requestCode,      
          int resultCode, Intent data) {
             super.onActivityResult(requestCode, 
             resultCode, data);
            if(requestCode == RC_SIGN_IN) {
               if(resultCode == RESULT_OK) {
                  //User is in !                 
           Log.d("Auth",auth.getCurrentUser().getEmail());
                } else {
                 //User is not authenticated
                  Log.d("Auth", "Not Authenticated");
              }
            } 
         }

Now we can build and run your android application. With that, you should have a fully functional firebase authenticating system in your android application.

Leave a Reply

Your email address will not be published. Required fields are marked *

Trending