Firebase Authentication For Android Made Easy

Esther Jolugba
5 min readApr 1, 2020

--

Firebase is a backend platform for building Android, iOS and web applications. It offers real time database, different APIs and multiple authentication and hosting platform.

I just concluded a mentorship program, She Code Africa Cohort as a mentee in Android track, for my final project which was a quiz app; I made use of Firebase for my backend Authentication. It was my first time of making use of firebase but it was worthwhile after all.

I will walk through how I was able to achieve it:

Prerequisites.

You will need a google account,Java or Kotlin knowledge,but for this tutorial I will be demonstrating with Java language, you must have Android studio Installed ,a good knowledge of some Android Concepts like XML, Intents etc. We are good to go now.

Create Project in Firebase Console

  1. Start by navigating to the Firebase Console webpage.
My Firebase Console

2.Select “ADD PROJECT” and name your project.

3. In this case, the name of my app is Booklovers, so let’s name the project Booklovers as well,.You can give yours any name.

4. Once your project is created, you can see the project’s Overview page.

Project Overview

Take a moment to look at the console home page for your project. You’ll see tabs on the left associated with many of the features. I’m going to need the Authentication feature for this tutorial. Feel free to click on the tabs and see where they lead.When you’re ready, move on to the next step in setting up your app.

Create a Firebase Project for Android

From your project page in the Firebase Console, select “Add Firebase to your Android App”. This opens a dialog window that asks for your app’s Package name,and the Debug signing certificate SHA-1. It also asks for an optional App Nickname, which you can keep blank.

For the package name, paste in your package name,mine is com.tinuade.booklovers so it matches the package name from the your android project. You can find this in your app's AndroidManifest.xml file.

Getting the Debug Certificate

You’ll need to add the debug signing certificate too ,which is optional.The SHA-1 is a type of hash representation for the debug keystore.You can do this by going to the gradle at the right side of your project screen.Select app>android>signInReport.Select the string of numbers and colons after the line that’s labeled “SHA-1:” and copy it. Then paste it in the dialog back in the Firebase Console.

SHA1

google-services.json

Once you click “Register App”, a google-services.json file should download automatically. The google-services.json file connects your client-side app with your specific Firebase project that will handle the server-side components of your app.

Once the download is complete, move the google-services.json file to the app directory of the your project. In Android Studio, you can select the “Project” view in the top-left corner of the Project navigation view, and then open the app directory. You can then drag the google-services.json file into the Android Studio project. You should end up with a project file tree like the following screenshot.

Add Firebase SDK

buildscript {
repositories {
// Check that you have the following line (if not, add it):
google() // Google’s Maven repository
}
dependencies {

// Add this line
classpath ‘com.google.gms:google-services:4.3.3’
}
}

allprojects {

repositories {
// Check that you have the following line (if not, add it):
google() // Google’s Maven repository

}
}

App-level build.gradle (<project>/<app-module>/build.gradle):

apply plugin: ‘com.android.application’
// Add this line
apply plugin: ‘com.google.gms.google-services’

dependencies {
// add the Firebase SDK for Google Analytics
implementation ‘com.google.firebase:firebase-analytics:17.2.2’

implementation 'com.google.firebase:firebase-auth:19.3.0'
implementation 'com.google.android.gms:play-services-auth:17.0.0'
//firebase Authentication dependency

// add SDKs for any other desired Firebase products

}

Sync your project to update the added SDK.

It time to explore the Firebase Authentication now,enable whatever Sign-In-Method you’ll like to use

Now go back to your project on Android Studio:

  1. Add Internet Users permission in your AndroidManifest.Xml.
  2. Get your Sign up and sign in Xml ready to suit your taste.
Here is mine

Finally you can start coding.

In your Sign up Java file:

Instantiate your variable ,make a global variable:

//widgets
private EditText mFullname, mEmailAddress, mPhonenumber, mPassword, mConfirmPassword;
private ProgressBar loadingProgressBar;
//Firebase
private FirebaseAuth mAuth;
mAuth = FirebaseAuth.getInstance();//Logic/authentication for your widgets goes here:You can also check my github repo for reference at here//Register User


mAuth.createUserWithEmailAndPassword(mEmailAddress.getText().toString(), mPassword.getText().toString())
.addOnSuccessListener(authResult -> {
//save user to the database
User user = new User();
user.setFullName(mFullname.getText().toString());
user.setEmail(mEmailAddress.getText().toString());
user.setPhone(mPhonenumber.getText().toString());

loadingProgressBar.setVisibility(View.VISIBLE);

//use phone as key

users.child(mAuth.getCurrentUser().getUid())
.setValue(user)
.addOnSuccessListener(aVoid -> Toast.makeText(Sign_up.this, "Registration Successful", Toast.LENGTH_LONG).show())
.addOnFailureListener(e -> Toast.makeText(Sign_up.this, "Registration Failed", Toast.LENGTH_LONG).show());
loadingProgressBar.setVisibility(View.INVISIBLE);

startActivity(new Intent(Sign_up.this, Book.class));
finish();
})
.addOnFailureListener(e -> Toast.makeText(Sign_up.this, "Authentication Failed: " + e.getMessage(), Toast.LENGTH_LONG).show());

}

I encourage you to have a model class like I did,Mine is User.java

In your Sign In Java file:

Instantiate your variable ,make a global variable:

//widgets
private EditText mEmail, mPassword;
private FirebaseAuth auth;
private ProgressBar loadingProgressBar;
//Firebase
private FirebaseAuth mAuth;
mAuth = FirebaseAuth.getInstance();// Check if user is already signed in
if (auth.getCurrentUser() != null) {
startActivity(new Intent(this, Book.class));
finish();
}
//Logic/authentication for your widgets goes here:You can also check my github repo for reference at here//Sign User In
loadingProgressBar.setVisibility(View.VISIBLE);
auth.signInWithEmailAndPassword(mEmail.getText().toString(), mPassword.getText().toString())
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
startActivity(new Intent(Sign_In.this, Book.class));
finish();
loadingProgressBar.setVisibility(View.INVISIBLE);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Sign_In.this, "Authentication Failed: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
})
;
}

You can check Firebase documentation for guidance at here

You can also check my github repo for reference at here

Feel free to drop your questions in the comment section.

Thanks for reading to the end:

Happy coding.

--

--