Firebase UI: Authentication using email/password FIREBASE

Firebase UI: Authentication using email/password  

Firebase UI: Authentication using email/password

Firebase UI: Authentication using email and password

As we have discussed previously, there are two authentication methods, i.e., Firebase UI and Firebase SDK. In this section, we will learn how to use the Firebase UI method to perform authentication using the email/password.

Firebase UI is an open-source library and very simple to use. We can customize it because it is open-source. It eliminates boilerplate code(section of code which has to be included in many places with little or no alteration) and promotes best practices both with the user experience and the security. A very simple API provides a simple drop and authentication.

It can handle the flow of all sources of different authentication techniques from email addresses and passwords to phone numbers type identity providers such as Google. It is built on top of the Firebase Auth libraries.

The best practice when we use this library is to maximize the sign-in and sign-up conversion for our application. It enables all automatic single tap signing for our app for returning users and also handles account recovery, account linking.

10 steps to understand the Firebase UI: authentication using the email/password:

Step 1:

In the first step, you need to connect your application with the firebase console. To connect the application with Firebase, go through the following link Firebase - Environment Setup (with android studio)

Step 2:

Once your application is connected to the Firebase, your next step is to import the required libraries for Firebase UI.

  1. implementation 'com.firebaseui:firebase-ui-auth:4.3.1'  
  2. apply plugin: ''  


Step 3:

In the next step, you need to set up or enable authentication methods. For this, you have to enable the email password authentication in the Firebase console.





Step 4:

Now, go to the Android Studio and create an activity for Sign-in and Sign-out.

Step 5:

Now, modify the file. You have to make sure that your app checks whether or not a user is already signed in from a previous session before invoking Firebase Authentication flow. For this, you have to check the Firebase instances and see if somebody is signed in.

  1. FirebaseAuth auth=FirebaseAuth.getInstance();  
  2. if(auth.getCurrentUser()!=null){  
  3.     //Already signed in   
  4. }else{  
  5.     //Not signed in  
  6. }  

Step 6:

Now you have to do is actually start signing using the Firebase UI libraries. The entry point for the authentication flow is the Auth UI class. You can retrieve an instance of the Auth UI by AuthUI.getInstances(). Now your need to create the sign-in code first which are as follows:

  1. //Creating a sign-in function which is called on button sign-in button click  
  2. public void sign_in(View view) {  
  3.     FirebaseAuth auth = FirebaseAuth.getInstance();  
  4.     if (auth.getCurrentUser() != null) {  
  5.         Toast.makeText(getApplicationContext(), "User already sign in signout first", Toast.LENGTH_SHORT).show();  
  6.     }else{  
  7.         //Choosing Authentication provider  
  8.         List<AuthUI.IdpConfig> providers= Arrays.asList(new AuthUI.IdpConfig.EmailBuilder().build());  
  10.         //Creating and launching sign-in intent  
  11.         startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder().setAvailableProviders(providers).build(),12345);  
  12.     }  

Step 7:

Now you need to create onActivityResult() because the flow for authentication UI has several response codes. In this function, and after this check the request and response code. If they match, the user will successfully signed-in.

  1. //Creating onActivityResult and checking response code to make sure we get the right request.  
  2. protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  3.     super.onActivityResult(requestCode, resultCode, data);  
  4.     //Checking for request code  
  5.     if(requestCode==12345){  
  6.         //Successfully signed-in  
  7.         if(resultCode==RESULT_OK){  
  8.             //Getting the current user  
  9.             FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();  
  10.             //Showing toast  
  11.             Toast.makeText(getApplicationContext(),"Successfully signed-in",Toast.LENGTH_SHORT).show();  
  12.         }  
  13.     }else{  
  14.         //Signed-in failed. If response is null the user canceled the sign-in flow using the back button.  
  15.         //respose.getError().getErrorCode() handle the error.  
  16.         Toast.makeText(getApplicationContext(),"Unabled to sign in",Toast.LENGTH_SHORT).show();  
  17.     }  
  18. }  


Step 8:

Now let's perform the sign out functionality. Because if a user signs in, then it is also required to have sign out. We will create a sign-out function in the following way:

  1. //Creating sign-out function which is called on button sign-out button click  
  2. public void sign_out(View view) {  
  3.     AuthUI.getInstance().signOut(this).addOnCompleteListener(new OnCompleteListener<Void>() {  
  4.         @Override  
  5.         public void onComplete(@NonNull Task<Void> task) {  
  6.             //User is now signed out  
  7.             Toast.makeText(getApplicationContext(),"User is signed out",Toast.LENGTH_SHORT).show();  
  8.         }  
  9.     });  
  10. }  

Step 9:

Apart from Sign-In and Sign-out, we can delete the user also. For this you need to implement delete() function which is called on button click. This will be as followed:

  1. public void delete_user(View view) {  
  2.     AuthUI.getInstance().delete(this).addOnCompleteListener(new OnCompleteListener<Void>() {  
  3.         @Override  
  4.         public void onComplete(@NonNull Task<Void> task) {  
  5.             if (task.isSuccessful()) {  
  6.                 Toast.makeText(getApplicationContext(), "User is deleted successfully", Toast.LENGTH_SHORT).show();  
  7.             } else {  
  8.                 Toast.makeText(getApplicationContext(), "Unsuccessful", Toast.LENGTH_SHORT).show();  
  9.             }  
  10.         }  
  11.     });  
  12. }  

Step 10:

This step is used for adding a new provider. Previously we used Email provider but we can add more providers such as Google, Phone number. This is done in the following way:

  1. List<AuthUI.IdpConfig> providers= Arrays.asList(  
  2.         new AuthUI.IdpConfig.EmailBuilder().build(),  
  3.         new AuthUI.IdpConfig.PhoneBuilder().build(),  
  4.         new AuthUI.IdpConfig.GoogleBuilder().build());  

For this, we have to enable the phone and Google provider in Firebase console.








Learn Programming for Free

Join Programmers Community on Telegram

Talk with Experienced Programmers

Just drop a message, we will solve your queries