In this video, let's implement the log in functionality.
So inside the user's controller and here we go, we are done with the registration, so let's collapse
it and let's work with the logging functionality.
So for the log in, let's look at the steps two.
Step number one is you are going to check if the email exists.
So here we go, check if email exists.
And after checking we want to compare if the password is correct.
So check for.
Password.
Validity.
So here we go less the structure, the body object as rig the body and what we need is the email and
then the password.
So here we go.
And when I be the same format as we did for the registration, so const user found is equal to await
the user model to find one and you want to find by the email field.
So if user found, it means that there is a user with this particular account.
So here we are going to invert.
Meaning if there is no user found, it means that this user is new or this user has to account with
us so we can go ahead and then return from this function.
And then you say That red dot, Jason, again, you find a proper way to handle error.
So let's say the message is equal to this.
For this one, don't try to be more descriptive.
For example, if the email is not found, don't say that.
Well, email is not found in that way.
We are giving a hint if a hacker is trying to use your account.
So provide a message that will not give more details about the response coming back if the password
or email is incorrect.
Most of times I like to use this invalid log in credentials.
Perfect.
So here after that, we're going to check for the password.
Remember, we are saving random string of characters of the password so we cannot remember those password.
So we are going to use the decrypt package to get that one done.
So let's see how we're going to implement that.
So the const is password match, meaning found match is equal to our weight.
Then you bring in the big package and the method is called per.
The name is even descriptive.
We pass in the plane password and then the sip password in our DB and that one we can get it on the
user found third password and this what is inside our database.
So for this it's going to return true or false for this function or method.
If it is true, it means that the password is correct and if it is false, it means that the password
is incorrect.
So here, let's check for that.
Meaning, if there is no password match, then we can go ahead and then send the same message so we
can copy this one and then paste it here.
Let's have some spacing between these ones as that and as that.
Perfect.
So at this point, everything is correct.
So now we can go ahead and then send the user details.
So here rest that saying let's send more meaningful messages by providing starters.
And equal to success.
And then let's say the full name also you going to be as user found dot full name.
And then let's also say the ID also is going to be user found dot.
And as crow eyed.
So moment of truth.
Let's go ahead and then look in.
So back to the ten lines.
This is the end point for logging in.
So here we go.
Let's provide the payload.
And the first one going to be the email.
And for the email, we use John.
John at gmail.com.
And then the password is equal to one, two, three, four, five.
Let's hit send.
And there we go.
But if I change the email, let's see.
And you can see we have invalid login credentials.
If the image is correct, but the password is incorrect.
We got the same response.
Meaning that everything is working fine.
So guys, that's how we implement the login function and it's the API.
What you have done is what you call the authentication, meaning allowing a user to log into our system.
But remember, HTTP servers are stateless, meaning that as soon as we look in our application does
not know or doesn't remember the log in user.
That's why we need to find a way to make HTTP servers more stateful by introducing what is called token
based authentication.
So in the next video, you have a look at how we can generate a token for the log in user.
So upon every request, the user will send that token to the roots with our we know that, okay, this
particular user is logged in.
Let's get into that in the next video.