Login with Email and Password – Firebase – Android

firebase_login_walkytechy

Login with registered email id and password – Firebase – Android

This is the second article on Android Firebase. In the previous article, we have seen how to integrate firebase to our Android project and how to do registration using Firebase. If you haven’t seen the previous article I would suggest you see the article Firebase registration using Email and Password. Because there we created a simple registration form in Android and in this post we are going to continue with that project.

Here in this post, we are going to learn how a user can login using registered email id and password.

firebase_login_walkytechy

So what we are going to do here is, we are going to continue from where we left in our previous article. When the user registration is successful, take the user to the login activity. If the login is successful a welcome screen will open with login successful message. And if it is not, we will show an error message saying what went wrong.

First of all, let’s create the login activity and its layout file. Go to Android Studio and create an empty activity. Name it whatever you want, I am naming it LoginActivity.

Now create its layout file. The layout file will have two EditTexts. One for the Email and the other one for the password. We also need a Login button. On successful login, the WelcomeActivity will open.

Below is the code of our LoginActivity and its layout.

Now to log in with email and password we have to initialize a FirebaseAuth instance. And to log in to an existing account there is a method called signInWithEmailAndPassword. This method takes two parameters same as the method createUserWithEmailAndPassword we used to register an account. The first parameter is for the email and the other one is for the password.

Below is the update code of our LoginActivity.

As you can see from the above code, that we are not doing any validation for our login form. As the main objective of this article is Firebase login, I am making this article as simple as possible. You should always do all the other necessary things like validation.

Now you can log in with your registered email id and password. But it will not show any confirmation on successful login. For that firebase has an on completion callback method. So we will use the method .addOnCompleteListener() to know if the login was successful or not.

Below is the complete code of our LoginActivity.java class.

From the above code, you can see we have used task.isSuccessful() to check whether the login is successful or not. If it is, then we are showing a toast message saying successful login and if it is not, then showing the user what went wrong.

If you want to open an welcome activity on successful login, just update your onComplete method with the below code –

Don’t forget to add the WelcomeActivity to your AndroidManifest.xml file. And also don’t forget to give internet permission if you haven’t done it already.

That’s all the code we need right now. Run your project, register an account. After successful registration, try to login with your registered email id and password.

Hope you understood everything in this tutorial. If you need any help or have a query related to this article, please don’t hesitate to comment below in the comment section.

 

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *