Introduction to Google Android Firebase Authentication
Most App need to know the identity of a user in order to securely save user’s data in the cloud and provide the same personalised experience across all of the user’s devices.
Firebase Authentication is a backend service provided by Google to enable developers to manage their backend, provides backend services, easy-to-use SDKs, and ready-made UI libraries to authenticate users to your app. It supports authentication using passwords, phone numbers, popular federated identity providers like Google, Facebook, Twitter and many more.
Nowadays, as a developer, leveraging on the tools and features made by others is a way of reducing the boiler plate code and increasing efficiency by saving us time and effort while ensuring productivity.
In Android Development, the Library ecosystem is as useful as anything an android developer would need in order to be successful. Also, knowing how to use libraries will make your working experience less cumbersome.
Using Sign–In is a feature that almost any internet enabled application or website supports. And because of its increasing popularity and extensive usage, Platforms like Google and Facebook have helped developers by making an easy-to-use tools for features like sign-in and give developers the ability to use social accounts such as Facebook, Google, Linkedin and many others for authenticating users in their platforms.
Today I’ll be showing us a quick way to implement the google and facebook sign-In for an android application. For this we’ll need the following
Ø A Fair Knowledge of developing android apps
Ø Android Studio(3.0 or higher)
Ø A mobile phone for testing or an emulator
Ø Internet
First off, we’ll start by creating a new android studio project and give it any name of our choice, then wait for a successful build.
After that we’ll go ahead to add the required dependencies for our application.
Since we will be making use of the firebase authentication, be sure to visit https://www.console.firebase.google.com and you’ll be logged in using your Gmail account if you have any, create one if you don’t.
There are two ways of adding firebase to our application, one of them is to do it manually using the guide here “Manually adding Firebase to our Application” the other is by using the firebase assistant in Android Studio.
To do this simply click on Tools => Firebase => Firebase Assistant and click on the Firebase Authentication and add email authentication, this will connect to your firebase account and to ensure this you have to be logged in to your Gmail account on Android Studio, just click on the email icon on the left. It will prompt us to after logging in, select an old project to add our current application to or to create a new project on firebase for it.
Once this is done, the necessary firebase dependencies will be added and synced, we can now head to our firebase console and click on authentication inside our current project and under the set Sign-In method we’ll enable google and facebook sign in, for more details using facebook authentication on the facebook developers page click here “ Link”
Next step will be to add the dependencies for FirebaseUi Authentication.
Firebase UI is a set of open-source libraries for Firebase that allows you to quickly connect common UI elements to the Firebase database for data storage, allowing views to be updated in real-time as they change.
To use the Firebase UI Authentication we’ll add the dependency
implementation ‘com.firebaseui:firebase-ui-auth:0.6.0’
also to use the firebase facebook sign in method we’ll have to use this dependency
implementation 'com.facebook.android:facebook-android-sdk:4.14.1'and head to the facebook developers section to register an account and obtain your
Facebook
Since support for Facebook Login is also required, define the resource string facebook_application_id to match theapplication ID in the Facebook developer dashboard:
<resources>
<! — … →
<string name=”facebook_application_id” translatable=”false”>APP_ID</string>
<! — Facebook Application ID, prefixed by ‘fb’. Enables Chrome Custom tabs. →
<string name=”facebook_login_protocol_scheme” translatable=”false”>fbAPP_ID</string>
</resources
Before invoking the FirebaseUI authentication flow, your app should check whether auser is already signed infrom a
previous session:
FirebaseAuth auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() != null) {
// already signed in
} else {
// not signed in
}
It is in our else condition that we invoke the firebase ui authentication using this line of code
startActivityForResult(AuthUI.getInstance() .createSignInIntentBuilder() .setProviders(AuthUI.GOOGLE_PROVIDER, AuthUI.FACEBOOK_PROVIDER) .build(), RC_SIGN_IN);
and the result of the Sign-In is checked for in our onActivityResult
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == RC_SIGN_IN){ if (resultCode == RESULT_OK){ //User successfully Signed in showSnackbarMessage("You are Succesfully Logged in"); FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); String displayName = user.getDisplayName(); String email = user.getEmail(); displayName_tv.setText(displayName); email_tv.setText(email); }else { checkLogin(); } }}
Here we got the display name and user email of the authenticated user and set it to a TextView.
You can do whatever you want with the details from the sign in method you decide to employ.
The link to this project is