‫Hello, my friends, and welcome back.
‫In this video, we're going to create the functionality for the main activity.
‫So we have to edit, text the email and password.
‫When the user enters the email and password and click on a login button, it will be going and checking
‫for the Firebase authentication service if it is correct or not.
‫If the email and password exist on the console or not, If the user needs to create a new account,
‫he will click on the Create account button and it will be directed to the sign up activity.
‫Okay, so let's add the functionality to this main activity.
‫At first we need to add and initialize the firebase authentication.
‫So private late initiate var oauth.
‫It's of type firebase auth.
‫Okay.
‫And here oth reference in the oncreate method I need to initialize it oth equal to firebase dot oth.
‫It's very simple.
‫We have initialized the Firebase authentication instance.
‫Now we can check in the Onstart method.
‫If there is, I will overwrite the Onstart method.
‫I will check if the user is signed in which is non null and update the UI accordingly.
‫So val current user equal to auth dot current user.
‫If it is, if the current user exists and not null, then what to do?
‫We are going to go to or go to.
‫Uh, list.
‫Journal.
‫List.
‫Journal List.
‫Activity, I will create this function.
‫It's nothing, but it will have an intent.
‫Var intent of type intent equal to intent that will direct us from the main activity.
‫This I can check like this and I can direct it to the journal list class dot Java and it will start
‫the activity.
‫Intent.
‫Okay.
‫This is just directing us to the journal list.
‫Now I need to add a sign up new user.
‫So here we have create account button.
‫If we direct, if we click on it, it will direct us to the sign up activity.
‫If we open it, we can see that we can create a user directly from here.
‫Now if the user exists and we need to log in.
‫So I need to add the functionality to the log in button.
‫So binding dot email sign in button.
‫This is the ID of the button dot set on click Listener.
‫And here I will add the the function called log in with email and password.
‫Okay.
‫This is the function that I will create.
‫Now.
‫It took into the parameter two strings.
‫The first one is the text and type string, and we need to get from the binding dot email, which is
‫an autocomplete text, autocomplete, edit text dot text dot to string dot trim.
‫And the second one is the password.
‫So password binding dot binding object dot password dot text dot two string dot trim.
‫Here we get the edit, text text and we are going to pass them to the login with email password Alt+.
‫Enter to create the function and here we have the function.
‫I will name the the arguments here, the or the parameters, the email and password.
‫Okay.
‫This is the login with email and password function.
‫Now it's very simple and it's similar to what we've done before in the sign up.
‫So I will use the authentication to to create like this.
‫I will go to here oath dot sign in with email and password.
‫This is a function that it is embedded and already used in the authentication.
‫And of course I can pass the.
‫Past parameters here to these two parameters.
‫Okay, Dot add Onsuccess listener I want from this function to tell me if it is succeeded or not.
‫On success or incomplete I can use on Oncomplete.
‫Listener I can pass this.
‫And I will choose this task.
‫So I need to pass the task as a lambda expression.
‫And here, if it is succeeded, so sign in.
‫Succeeded?
‫What I need to do.
‫Success.
‫I need to update the UI, which is going to the which is directing the user to the journal list and
‫I will create Val user equal oath dot current user and I will call this method go to journal list.
‫Go to journal list.
‫Otherwise, if it is not succeeded what I need to do.
‫This is.
‫This is the task.
‫If task.
‫It is successful.
‫I need to do this.
‫Okay.
‫Otherwise, if it is not succeeded, what I need to do, I need to display a toast message telling the
‫user that there is an authentication failure.
‫So toast dot make text.
‫The first parameter is.
‫This.
‫The second parameter is authentication failed.
‫And the third parameter is toast.
‫Dot length long.
‫And show message.
‫Here.
‫Okay.
‫This is what all we need from this main activity.
‫Now let's clean the project.
‫And rebuild it and run the application.