Firebase User Authentication In Android - Part One

Android
 

Introduction

 
Firebase provides many features as described in my previous post. One of the most useful features is User Authentication. Different types of User Authentication are:
  1. Email and Password Authentication
  2. Google Plus Authentication
  3. Facebook Authentication
  4. GitHub Authentication.
  5. Twitter Authentication
  6. Phone Number Authentication
  7. Anonymous
  8. Play Games Authentication.
In this post, we will learn how to implement Firebase Email & Password authentication. To demonstrate how simplified and easy to use Firebase is, we will build a simple login/register (Firebase Authentication) demo. This separates sensitive user credentials from your application data and lets you focus on the user interface and experience for your Application. It is a simple and suitable way of handling Login, Registration, Forgot Password, and so on.
 
After creating the project, enable Firebase Email & Password authentication by selecting Authentication in the left pane of the  Firebase Console and go to Sign-in-Method Tab as in the following Image.
 
Android
 
Getting started with Firebase
 
Read the following to know how to set up the Firebase in your Android project.
 
Reference
 
https://androidmads.blogspot.in/2016/10/android-getting-started-with-firebase.html
 
Steps
 
I have split this part into 3 steps as in the following.
  • Step 1: Creating a New Project with Empty Activity.
  • Step 2: Setting up the Firebase Library.
  • Step 3: Implementation of Email & Password Authentication with Firebase.
Step 1 - Create a new project with Empty Activity
  1. Open Android Studio and select Create a new project.
  2. Name the project as your wish and select Empty activity.
     
    Android
     
  3. Click the “finish button to create a new project in Android Studio.
Step 2 - Setting up the Firebase Library
 
In this part, we will see how to set up the library for the project.
  1. Open your project level build.gradle file and add the following lines in dependencies
    1. {  
    2.     …  
    3.     classpath 'com.google.gms:google-services:3.1.0'  
    4.               …  
    5. }  
  1. Then add the following lines in allprojects in project level build.gradle file.
    1. allprojects {  
    2.     repositories {  
    3.         google()  
    4.         jcenter()  
    5.         maven {  
    6.             url "https://maven.google.com"  
    7.         }  
    8.     }  
    9. }  
  1. Then add the following lines in app level build.gradle file to apply google services to your project.
    1. dependencies {  
    2.     ...  
    3.     implementation 'com.google.firebase:firebase-auth:11.8.0'  
    4. }  
    5. apply plugin: 'com.google.gms.google-services'  
  1. Then click “Sync Now” to setup your project.
Step 3 - Implementation of Email & Password Authentication with Firebase
 
In this part, we will see how to implement Email & Password Authentication with Firebase & in this step, we will see many features of Firebase.
 
SIGNUP METHOD
 
In Firebase Authentication, the credits entered by user might have some validation. For Example, Email ID might be in proper email format and Password must have at-least 6 characters. Firebase Provides createUserWithEmailAndPassword() to create New User in Firebase.
  1. // Validating Credits entered by User with Firebase  
  2. btnSignUp.setOnClickListener(new View.OnClickListener() {  
  3.  @Override  
  4.     public void onClick(View view) {  
  5.   final String email = inputEmail.getText().toString();  
  6.   final String password = inputPassword.getText().toString();  
  7.   try {  
  8.    // Email ID must be valid  
  9.    // Password strength is minimum 6 characters by default in firebase registration  
  10.    // Minimum Password length throws Error as 'WEAK PASSWORD'  
  11.    if (password.length() > 0 && email.length() > 0) {  
  12.     PD.show();  
  13.     //authenticate user  
  14.     auth.createUserWithEmailAndPassword(email, password)  
  15.      .addOnCompleteListener(RegisterActivity.thisnew OnCompleteListener() {  
  16.       @Override  
  17.       public void onComplete(@NonNull Task task) {  
  18.       if (!task.isSuccessful()) {  
  19.        Toast.makeText(  
  20.        RegisterActivity.this,  
  21.        "Authentication Failed",  
  22.        Toast.LENGTH_LONG).show();  
  23.       } else {  
  24.        Intent intent = new Intent(RegisterActivity.this, MainActivity.class);  
  25.        startActivity(intent);  
  26.        finish();  
  27.       }  
  28.       PD.dismiss();  
  29.      }  
  30.     });  
  31.    } else {  
  32.      Toast.makeText(  
  33.       RegisterActivity.this,  
  34.       "Fill All Fields",  
  35.       Toast.LENGTH_LONG).show();  
  36.    }  
  37.   
  38.   } catch (Exception e) {  
  39.    e.printStackTrace();  
  40.   }  
  41.  }  
  42. });  
SIGNIN METHOD
 
Firebase provides signInWithEmailAndPassword() method to sign in the user.
  1. // Validating Login Credits with Firebase  
  2. btnLogin.setOnClickListener(new View.OnClickListener() {  
  3.  @Override  
  4.  public void onClick(View view) {  
  5.   final String email = inputEmail.getText().toString();  
  6.   final String password = inputPassword.getText().toString();  
  7.   try {  
  8.    if (password.length() > 0 && email.length() > 0) {  
  9.     PD.show();  
  10.     // Authenticate user  
  11.     auth.signInWithEmailAndPassword(email, password)  
  12.       .addOnCompleteListener(LoginActivity.thisnew OnCompleteListener() {  
  13.        @Override  
  14.        public void onComplete(@NonNull Task task) {  
  15.         if (!task.isSuccessful()) {  
  16.          Toast.makeText(  
  17.            LoginActivity.this,  
  18.            "Authentication Failed",  
  19.            Toast.LENGTH_LONG).show();  
  20.          Log.v("error", task.getResult().toString());  
  21.         } else {  
  22.          Intent intent = new Intent(LoginActivity.this, MainActivity.class);  
  23.          startActivity(intent);  
  24.          finish();  
  25.         }  
  26.         PD.dismiss();  
  27.        }  
  28.       });  
  29.    } else {  
  30.     Toast.makeText(  
  31.       LoginActivity.this,  
  32.       "Fill All Fields",  
  33.       Toast.LENGTH_LONG).show();  
  34.    }  
  35.   } catch (Exception e) {  
  36.    e.printStackTrace();  
  37.   }  
  38.  }  
  39. });  

Summary

 
In this part, we have learned how to do SIGN-IN & SIGN-UP with Firebase User Email & Password Authentication. In the next article, we will see the additional features or services provided by Firebase.


Similar Articles