Android Getting Started with Firebase – Login and Registration 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 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
Post a Comment