Skip to main content

Firebase – Login and Registration Authentication

Android Getting Started with Firebase – Login and Registration Authentication


With the latest news from Google I/O comes the new and upgraded Firebase. To demonstrate how simplified and easy to use firebase is, we will build a simple login/register (Firebase Authentication) demo using the Firebase Email & Password authentication.

Firebase provides a full set of authentication options out-of-the-box. Firebase automatically stores your users’ credentials securely (using bcrypt) and redundantly (with replication and daily off-site backups). This separates sensitive user credentials from your application data, and lets you focus on the user interface and experience for your app.

Features of Firebase

Firebase comes with bunch features essential for every android app starting from authentication to hosting the app.

Firebase Advantages

* Super easy and quick to implement 
* No server side configuration needed. No PHP Scripts and No Database Designs 
* Real-time update without using GCM 
* Auto-scaling built-in 
* Can start for free (only need to start paying once we hit 50 connections) 
* Robust APIs for JavaScript (including several frameworks like Angular), iOS, and Android 
* Built-in support for authentication services like Facebook, Google, and Twitter 
* Declarative Security Rules model allows us to enforce read/write privileges and data validation throughout the tree


Firebase Disadvantages

* Need to build indexes manually 
* May need to build "event log" manually as well (in separate sub-tree?) 
* Implementation of REST API could be difficult on embedded platforms 
* Data validation rules do not support complex objects directly (you'd need to validate individual child nodes separately)

1. Enabling Firebase Auth

* First thing you need to do is go to Firebase and make an account to gain access to their console. After you gain access to the console you can start by creating your first project.
* Give the package name of your project (mine is com.ravi.firebase) in which you are going to integrate the Firebase. Here the google-services.json file will be downloaded when you press add app button.
* Next go to your project dashboard. Find the Auth and click get started. Go to set up sign in method and choose Email & Password and enable it.





Now we are ready to start with our Android project. We are going to create a simple app which contains firebase authentication and profile management. Overall we are going to see how to add Login, Registration, Forgot Password, Change Email, Change Password & finally Sign Out option.

2. Creating Android Project

Create a new project in Android Studio from File ⇒ New Project.
When it prompts you to select the default activity, select Blank Activity and proceed
While filling the project details, use the same package name which you gave in firebase console. In my case I am using same com.ravi.firebase

Open AndroidManifest.xml and add the INTERNET permission as we need to make network calls

<uses-permission android:name="android.permission.INTERNET" />

Paste the google-services.json file to your project’s app folder. This step is very important as your project won’t build without this file
Now open the build.gradle located in project’s home directory and add firebase dependency

build.gradle
dependencies {
       classpath 'com.android.tools.build:gradle:2.1.2'
       classpath 'com.google.gms:google-services:3.0.0'
      // NOTE: Do not place your application dependencies here; they belong
      // in the individual module build.gradle files
}

Open app/build.gradle and add firebase auth dependency. At the very bottom of the file, add apply plugin: 'com.google.gms.google-services' app/build.gradle

dependencies {
compile 'com.google.firebase:firebase-auth:9.0.2'
}
apply plugin: 'com.google.gms.google-services'

* Add the below resources to dimens.xml, colors.xml and strings.xml. These resources doesn't require for firebase, but for this article

Now we have the project ready with all the dependencies added. Let’s start by adding the sign up screen

2.1 Sign Up with Email & Password

Create an activity named SignupActivity.java and add the following code to the layout file activity_signup.xml

2.2 Log In with Email & Password

Now we'll add the login screen and check the credentials we have created on sign up screen
Create another activity named LoginActivity.java and add the below code to its layout file activity_login.xml
Open AndroidManifest.xml and make the LoginActivity.java as launcher activity to make the login screen as first screen
Run the project and login with the credentials which you used while signing up


2.3 Forgot Password – Send Reset Password Email

Create another activity named ResetPasswordActivity.java and add the below code its layout file activity_reset_password.xml
Here is the password reset email user will receive


2.4 Profile Screen

Create another activity named MainActivity.java and add the below code its layout file activity_main.xml
Put Checking User Session, Change Password, Change Email, Deleting Account/User, and Sign Out functionality in main activity
Now you have the basics for creating a simple login / register app with firebase. You can continue exploring firebase as it offers much more functionalities and it’s fun to use them.

Comments

Popular posts from this blog

How to finish all activities in your Android application through simple call

Hi, Maybe you had some problem which I can describe and show the way to fix it. Actually, problem is a closing all your Android application’s Activities through simple call. There are many ways to fix this. I want to show you one of them. Before starting I want to ask you something. Please email me or post a comment for this post if you find better and more flexible way to close all Android application's activities. I will be appreciative you! Below you can download the sample project. This project contains 3 (three) activities and ONE abstract Activity. All Activities in this sample project extended this abstract Activity class. This Activity extended Activity class from Android API. The most interesting contains in this abstract Activity. Here the source: package com.ravi; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; public class AppBas

ORMLite with Android

This tutorial will guide you how to implement ORMLite (a very light weight, open source, Android compatible ORM) in an Android project. Android supports SQLite database and writing database queries can create a lot of boilerplate code and can be really difficult to debug. I was looking for some sort of ORM library for android. I came around few of them. I decided to give ORMLite a shot. The library is very stable and uses annotations. It was really easy to implement everything so I decided to stick with it. What are the benefits you may achieve compare to conventional way of using SQLite from Android. You will learn to build an app with ORMLite that allows you to store and view data from a database. So lets begin… Model To use ORMLite, you first need to create your model classes. A sample model class would like this. Open below link to view source EmployeeDetails.java Database Helper It is the most important file from the ORMLite implementation point of view, which cons