‫Welcome back.
‫We succeeded in creating the accounts and creating the signup functionality for our app.
‫Now let's add the login functionality in order to allow the user to log in that it is registered in
‫our database and be directed to the journals list activity.
‫I'll start by the main activity here and I need to initialize the widgets, the login button and the
‫edit text.
‫Let's start with the Firebase authentication.
‫I'll initialize a new variable here called Firebase auth.
‫So let's go up and start with the Firebase auth.
‫Here I'll declare the global variables exactly what we've done in the sign up activity.
‫We need the listener.
‫And we need the user for the current user.
‫Let's go down in the Oncreate method.
‫I start with the firebase auth equals firebase auth dot get instance.
‫Firebase auth is a class provided by Firebase that represents the entry point for interacting with Firebase
‫Authentication Services.
‫It allows you to perform operations related to user authentication such as creating accounts, setting,
‫signing in, signing out and managing user profiles.
‫Firebase authentication dot get instance method is a static method that receives and retrieves an instance
‫of Firebase auth class.
‫It ensures that you're working with a single shared instance of Firebase auth for your entire application.
‫This is a best practice as it ensures consistent authentication state and avoids unnecessary resource
‫consumption.
‫Then we need to set onClick listener for the login button.
‫New view on click listener.
‫And by the way you can replace it with Lambda.
‫So I'll remove this onclick here and replace it with this lambda set on click listener.
‫I need to call a method called login email password user.
‫This method will take into the parameter two strings, one for the email and one for the password,
‫and we get them from the edit text.
‫Trim method is used to remove any extra lines.
‫Login.
‫Email.
‫Password.
‫User.
‫This function will be created here on outside the Oncreate method.
‫So private void login email password user and this method takes two parameters.
‫Now I want from you to focus with me because we're going to check for login.
‫So if the user exists in our Firebase authentication system, it will be directed to another activity
‫and the login will be successful.
‫If not we're going to display an error message.
‫I'll check for empty texts.
‫So if text util is empty email and password.
‫So if the email and the password are not null or not empty, we're going to try to sign in in our Firebase
‫authentication system.
‫So Firebase dot sign in with email and password.
‫This is a method provided by the Firebase Authentication package.
‫And we can pass the email and the password to check for their existence.
‫So I need to pass the email that it's passed by the parameter here and the pwd which is the password.
‫Then I need to add an Oncomplete listener new Oncomplete listener.
‫And the Oncomplete method will tell us if the sign in is executed successfully.
‫After the success I need to get the Firebase user equals to Firebase auth dot get current user.
‫I need to assign the Firebase user here and use this globally in our application.
‫And I'll create a new activity Empty Views activity and I'll name it as Journal List Activity.
‫This activity will display all the journals in our Firebase Firestore.
‫Click finish and back to the main activity.
‫If the login is successful I need to direct the user to this activity here.
‫Intent I equals to new intent I need to direct it from the main activity dot this or this directly to
‫the journal list activity dot class, start activity.
‫And here I need to pass the main activity since I'm using it in this method.
‫Main activity dot this and this is good.
‫Here in the journal list I'll create a text just to understand that it is the journal list.
‫In further constraints.
‫And I'd like to hear.
‫Okay, just I'm telling Android Studio to create this simple activity with one text view in order to
‫understand that we are in the journal list activity.
‫Stop the application, then run it again.
‫Now let's try to log in the email.
‫I'll start with not registered email login and you can see that it is not existed, but it will direct
‫us to the journal list activity.
‫This is due to the wrong listener that we are implementing here Oncomplete.
‫So whenever this will be executed, then go and execute this line.
‫I don't need like this, I need to use Onsuccess listener.
‫This Onsuccess listener will check for success.
‫So if the user exists, we need to execute this code.
‫Okay.
‫Run again.
‫Enter any email, any password login.
‫Nothing happened.
‫Now let's try to add my real email that I logged in in this activity.
‫Login and you can see that we are directed to the journal list activity and make sure you implement
‫the correct listener.
‫Add Onsuccess listener is different from add Oncomplete listener.
‫And this is what I need to tell you.