In this tutorial, I’m going to show you how to create an app with android and connecting firebase to Authenticate the user. In the previous tutorial, I have done how to Configure firebase on your android project. Anyway, I’m going to do it from the beginning so anybody can understand each and every step.
Create an empty Android project. Then go to the Firebase console. From the console Add and give the project name as shown below and press continue.
Then you will be redirected to the dashboard of the Firebase. On the dashboard select android icon to configure Firebase with your project.
You have to add the Android Package name for this. You can easily find the package name from the project you created from the android studio. Open your created project. Then open Android Manifest file. Then you can find your package name. copy and past the name to the Firebase. There are other two entries which are optional to register the app in Firebase. If you wish you can add them.
Next step is downloading configured json file.
After you downloaded this file from the Firebase you have to add it to the android project app folder as shown in this figure.
Then, you have to add some code stuff for two files. That is adding dependencies to your gradle file. Change your app level build.gradle file and project level build.gradle file. Do not confuse those two files.
Project-Level build gradle. Inside the dependencies
App-level build gradle
Now you have done configuring Firebase. Still, there is one more work with the Firebase. Go to the Authentication Tab which you can see on the side menu bar of the Firebase dashboard. Then go to the Sing-in methods. In here you can see several ways how you can authenticate the user with Firebase. From there select Email/Password and enable it
Now Firebase work is done.
Go to the project and create.We have to design the interface for the user Registration. Insert the following code for the activity_signup.xml page
Then create a java class called SignUpActivity.java and insert the following code
Before we go to the next step let’s discuss what happened in the SignUpActivity.java file. onCreate of the SignUpActivity is getting the Firebase reference. When the user clicks the signUp button it checks email and password field and makes sure they are not empty. If so it gives an error. Then it calls a createUserWithEmailAndPassword(email, password) which is a Firebase function. From this, it creates a user with the supplied email and password. If the response for the request is successful it will navigate to the MainActivity page.
Now we are going to design the screens for the user to login.create a layout file as activity_login.xml and add the following XML code to it.
Now create a java file called LoginActivity.java to handle the login functionalities
Before going to the other part I’ll explain what happens in the above code.
Inside the OnCreate() method we are getting the Firebase instance by FirebaseAuth.getInstance() where the mobile can understand which Firebase connected with the mobile app. After that Rendering, the login_page is done. Then we are giving click listeners for a login button, forgot password button and register button.
when the user clicks the login button it first checks the email and password is empty. If so it won’t allow to the login process. If the email and password entered it call the signInWithEmailAndPassword(email, password) which is a derived method in Firebase for login. It sends a request to the Firebase and checks whether a user exists with entered email and password. If so it allows the user to authenticate and go to the main page. Otherwise, it generates a message saying the cause error.
Most of the work is done. But what if the user forgot the password which he entered during the registration process. So there should be a way to reset the password. This screen is design for manage that password resetting thing. create a layout called activity_password_reset.xml and insert the following code.
Then create the java class called PasswordResetActivity.java and enter the following code
Here the main function is sendPasswordResetEmail(email) which is sending an email from Firebase to the user. From the email, the user can reset the password.
This is the final Step. So far the user can signup login and request password resetting email. In here we are creating the home page which contains the following Functions
For implement that inserts the following XML code to the activity_main.xml file
Then change the MainActivity.java as follows.
Now, all done. Run your app and test. You can see an app like below.
In this tutorial, we discuss how to use Firebase authentication to handle major functionalities such as signup, login, resetting password and so much. You can use this app as the foundation of building your other creative apps. If you having trouble with the code you can see my project source code in the git hub.