Add Firebase Authentication to your App in 7minutes
Tags: firebase, android, authentication, mobile
Published: 2017-10-17 12:27:07

---
title: Add Firebase Authentication to your App in 7minutes
published: true
description: 
tags: Firebase, Android, Authentication, Mobile
---
![Registeration Layout](http://res.cloudinary.com/kennyy/image/upload/v1508241569/Firebase_0_dybayu.png)
Based on popular demand from developers looking to build their stuff on firebase, I’ve put this tutorial together to help with the basics of firebase authentication.
First off, you need to create a Google account “if you don’t have one” then use it to create a firebase account at console.firebase.google.com. When your firebase account is ready, then connect it to your app.
I thought of different ways to handle this topic to bring out the whole essence of it and make sure you understand it clearly so I arrived at building a small firebase app that lets users signup, with their signup credentials, login with those credentials and retrieve as much information as they want.
Hopefully by building this app, you’ll learn how to handle authentications seamlessly, so either build along with me or just read along, either way, you’ll learn it. For my web friends, i’ll continue this article for web on my next release; right now I’ll just continue building from android studio.
So create a new android studio project called “FreeStyle” (if you can’t or still want to learn how to create a new project in AS, see my article on Parsing remote JSON, I explained how to do it in details).
Next is to connect your app to firebase, there are more than one way to do this but I’ll stick with the way I prefer amongst other ways and that is, in your android studio
Ã˜ Click on the tools tab
Ã˜ Click on firebase
Ã˜ Click Authentication and
Ã˜ Click connect to firebase
After it has created the firebase project,
Click on “Add firebase Authentication to your project”
Note: your system has to be online for this part and if you’ve not done this before, it’ll probably ask for your firebase credentials so just supply it and proceed.
Once your projected is connected to firebase, you’ll get a prompt to allow firebase make changes to your gradle files, once you allow it, it’ll automatic add the firebase dependency to your build.gradle file and Boooom!! Your firebase app is ready, congrats!! ðŸ˜€ðŸ˜€ðŸ˜€
You know it’s really good practice to smile and be happy when you accomplish something in your code, I mean, we can’t see it yet so it’s totally cool if you appreciate yourself every once in a while.
Now that you’ve had fun with yourself, let’s start creating the necessary layouts, first we’ll create the Registration layout. It’ll have 2 input fields; say an email field and a password field. This part should be fairly simple for everyone but for the sake of completeness, I’ll walk you through how to do this. Since you already have activity_main.xml, let’s just work on it instead of creating a separate layout file for Registration.
So open your activity_main.xml file and type in this code

![Registeration Layout](http://res.cloudinary.com/kennyy/image/upload/v1508241582/Reg1_1st_aqmfqx.jpg)
![Registeration Layout](http://res.cloudinary.com/kennyy/image/upload/v1508241566/Reg2_2nd_h2s96x.jpg)
You should pay more attention to the id’s you assigned to the view objects because you’ll use them for reference in other Activities but if you used exactly the same id’s as i did in the snippet above, then you’ll be just fine.

![Registeration Layout](http://res.cloudinary.com/kennyy/image/upload/v1508241579/reg_3rd_hgm29s.jpg)
Now when the user fills up the fields and clicks the register button, their credentials should be stored in firebase and upon successful registration, the ProfileActivity will be launched so they can view their details as it’ll be displayed in the Profile page.
So in the MainActivity.java file we declare instances of the view objects and that of the FirebaseAuth;

![Registeration Layout](http://res.cloudinary.com/kennyy/image/upload/v1508241583/4th_lmetu8.jpg)
Now let’s create a method that registers the user. First we’ll get() the values of the input fields and store them inside String variables so that we can use them as Strings to create the user in the firebase createUserWithEmailAndPassword() method.
Next we set conditions to check for empty or malformed input fields such that users cannot not register with malformed email addresses or empty password/email input fields.
Then finally to create the user on firebase, we call the createUserWithEmailAndPassword () method on the FirebaseAuth Instance and implement the onComplete () method. Inside the onComplete () method we’ll decide what happens when the user is successfully registered or when the registration fails. In my case, if the user registers successfully, then I’ll make a Toast (registration successful) and launch the users profile, if it fails, I’ll just make a Toast (registration failed). However I’ll surround everything inside the onComplete() method with “try and catch” since it’ll be performing network functions.
![Registeration Layout](http://res.cloudinary.com/kennyy/image/upload/v1508241569/RegUserMain_5th_idkfls.jpg)
Then on the register button, we set an OnClickListener to call the registerUser() method. If the user is already registered, we also set an OnClickListener on the login button to start the LoginActivity when the button is clicked.
![Registeration Layout](http://res.cloudinary.com/kennyy/image/upload/v1508241566/ButtonReg_6th_ophpid.jpg)
So let’s now create the ProfileActivity class, right click on java and select New>Activity>Empty Activity and name it ProfileActivity.
In the activity_profile.xml file, create the view objects that will hold the data that you’d like to display in the ProfileActivity. In my case, I want to display their email and user id. Hence, i’ll have 2 TextViews with their corresponding id’s.
![Registeration Layout](http://res.cloudinary.com/kennyy/image/upload/v1508241583/ProfileLay1_7th_hulr4p.jpg)
![Registeration Layout](http://res.cloudinary.com/kennyy/image/upload/v1508241584/ProfileLay2_8th_g2cvl0.jpg)
In the ProfileActivity.java file, declare the FirebaseAuth instance and initialize it in the onCreate() method as we did for the MainActivity class.
Since we’ll be displaying the users information, we’ll also need to declare FirebaseUser and instantiate it as well as the instances of the view objects.
Then we display the users details on the view objects we created on the layout file.
Thus:
FirebaseUser user = mAuth.getInstance().getCurrentUser();
If (user!= null){
String email = user.getEmail();
String Uid = user.getUid();
}
![Registeration Layout](http://res.cloudinary.com/kennyy/image/upload/v1508241578/ProfileMain_9th_iald8x.jpg)
![Registeration Layout](http://res.cloudinary.com/kennyy/image/upload/v1508241584/profile_10th_jg36nt.jpg)
i also added a logout button here so that the user can log out and return to the LoginActivity when they are done viewing their profile. it’s fairly simply to do, just add a button on the layout file, give it an id say button_logout. in the ProfileActivity class just initialize it and set an OnClickListener on it. the onClick method should do if(user !=null){
mAuth.signOut
}
LoginActivity
Create another Activity called LoginActivity (do this the same way we created the ProfileACtivity(). Open the activity_login.xml file and create input fields for login and password.

![Registeration Layout](http://res.cloudinary.com/kennyy/image/upload/v1508241573/LoginLay1_11th_fv9tcy.jpg)
![Registeration Layout](http://res.cloudinary.com/kennyy/image/upload/v1508241575/LoginLay2_12th_uod2up.jpg)
Next we open the LoginActivity.java file and declare instances of the view objects and the FirebaseAuth. Then initialize them in the oncreate() method as we did for the ProfileActivity.
![Registeration Layout](http://res.cloudinary.com/kennyy/image/upload/v1508241575/LoginMain1_13th_jj60sh.jpg)
Now let’s create a method that’ll login the user. First we’ll get() the values of the input fields and store them inside String variables so that we can use them as Strings to sign the user in to firebase with the signInWithEmailAndPassword() method.
Next we set the signInWithEmailAndPassword() method on the FirebaseAuth instance and implement the onComplete() method. Inside the onComplete() method we’ll decide what happens when the user is successfully signed in or when the sign in fails. In my case, if the user signs in successfully, then I’ll authenticate the user and launch the profileActivity, if it fails, I’ll just make a Toast (couldn’t login).
![Registeration Layout](http://res.cloudinary.com/kennyy/image/upload/v1508241574/LoginMain5_14th_tcsqf8.jpg)
Then on the login button, we set an OnClickListener to call the loginUser() method.
![Registeration Layout](http://res.cloudinary.com/kennyy/image/upload/v1508241568/LoginButton_15th_jstbsp.jpg)
![Registeration Layout](http://res.cloudinary.com/kennyy/image/upload/v1508241569/login_16th_noegyb.jpg)
Note before you run the app, you’ll need to set Internet permisions in the AndroidManifest.xml file and also enable Email/Password in your firebase console as the SignIn Method.
Secondly, to see the users who have successfully registered on your app, log in to your firebase console, click Authentication and click the users tab.
![Registeration Layout](http://res.cloudinary.com/kennyy/image/upload/v1508241568/add_user_17th_gyjkl2.jpg)
There are a lot more features you can implement, like storing data in the firebase database, retrieving it, adding more fields to the registration input fields and retrieving them etc. this tutorial is only serving the purpose of authentication to enable your users SignUp and SignIn into your app with their credentials. You can equally extend it to add email verifications, Twitter, Facebook or Google Sign in methods etc.
Like always, I’m available (twitter @kenny_io) to provide more clarifications and answer your questions if you have any. 
The source code is also available on github if you need it.