- [Instruction] So the final function that we can use for
our user controllers is the login function.
So basically allowing to login into the API's
and once we're logged in, then we can go ahead
and do transactions with the API.
So let's go ahead and do that function
so we're going to call this log in.
So you create a new function
and then the arguments again
are the request and the response.
And then what we're going to do is go ahead and try to find
a user in the database.
So findOne.
So basically, what we need to do is
grab the email from the request .body
.email.
So again in the request, we're going to have an email
that we'll try to match to
something that we have in our database.
So what we'll do and you want to go after the curly brace
before the parenthesis, is do a comma
and then go ahead and do an error and user
and then what we'll do is keep going with a new function
to check if there's a new user.
So if we get an error,
we'll throw the error.
Then if
if there's no user in the database,
we're going to do a response of status
401
and we'll send a response in a json format as well,
so we'll do a message that will say
authentication
failed.
No user found.
And then what we'll do is go outside
of this curly brace here,
so you want to make sure that you go just after
what we'd just written and right here
and then what we're going to do an else, if,
user, so if we find a user,
and then inside.
So remember, we created a function to compare the passwords
so this is where we're going to use it.
So if we found a user,
then let's make sure that the passwords match.
So if
the user compare
password
from the request, again it takes two arguments.
So the first argument is the password that is being passed
from the request, which is what I've explained
in the previous video.
And as the second argument,
user.hashpassword,
so we're basically comparing the password from the database
to the one passed inside of the request.
And then continue with a function.
If they don't match, so this is the negative piece,
so if the user compare password doesn't match
then you do the response as follow.
So we're going to do again a json message,
that's authentication fail,
wrong password.
Now the last piece that we need to do
is if, so the last thing we need to do,
is if everything else has been checked,
then that means that we have a user
and the password matches
and then we can send a token from jwt
and we'll sign it.
So basically, we're sending a token back to the response
and telling the request we found somebody,
the passwords match, here's the token,
you can use this token to actually make transactions
with the APIs.
So what I'm going to do in the token,
I'm going to sign this user
email, username,
with the user.username.
Then also do the same, and let me just do a quick word wrap
so we can see everything on the same page here.
So let's go ahead do the score id
that we'll get from this user
and use user id.
And then we're going to sign this with a word.
So this could be any word,
but you want to make sure that you're using
the same word from the request to the actual signature
and the API is here.
And you'll understand this once we use that word again.
So for my Kasie, I'm going to call this one restful APIs
and it could be anything so it's basically the secret word
that json with token is going to use to sign your token.
Okay?
So we're all good.
Okay, so one thing that I wanted to make sure
is that there's no typos anywhere
that will basically break the application
and actually I see one right here so
password shouldn't be like that so let's
fix this and save.
And everything else seems to be fine, okay.
So, now that we have all these functions completed,
let's move on.