In the last lecture, we added user sign up,
now I want to add user sign in. Now for user sign in, a request will reach this route here in the node app
and there, I also get the email and password with which the user tries to sign in
and now we need to write some logic to check whether that is some valid data
and for that, I first of all need to try to retrieve a user by that e-mail from inside my database.
So to do that, I get access to my database, call db here
and then collection and get access to the users collection and then there, I want to find a user. Now for
that, I want to find one element because well there should be only one user for that email anyways if
at all and I'll search for the e-mail,
so a good thing that we created the index and pass my e-mail which I extract here as a value.
So I'll search for a user with that e-mail
and then I either get an error or I get well a user. In the case of an error, I will return this error here,
a 401 error,
in the case of a success, I'll get my user document here.
But now we're not done,
I still need to verify whether the password is correct
and for this, I'll use the bcrypt package which I also used for hashing and there, there is a compare method
and compare simply takes a string as an input and compares whether this can be the valid password basically.
So I pass in the password which I extracted from the incoming request and I pass in the hashed password
which is stored in a database and then this package compares whether the hashed password could be based
on that password the user passed in,
so here I'll pass in the user doc password field,
so basically that will be this hashed value here.
So this is what I pass in and I will actually return this because bcrypt compare also returns a promise
and if I return a promise in a then block, then I can simply chain another then block and I could
have done that down here too by the way because collection insert one returns a promise,
so I could have returned this entire block and then simply added this then block after this first then
block. You can do that if you want,
here I will do it,
I return the compare function,
then I add another then block and in here, I know that we found a user with that e-mail and that the
password is valid.
So in here, I don't really care about the result but I know that it should be correct,
so let's grab that response and let's send a success response here with a message of authentication
succeeded
and now here, I'll also create a token,
so I'll take that token constant, create that before I send the response and then include that token
in the response
and now let's see if that works or if there is some gotcha.
So if I now
restart my backend, let me switch to log in and let's try logging in with a wrong email and I get an
error.
Now let's try logging in with the correct e-mail but a wrong password
and this succeeds
and the reason for that is that compare doesn't throw an error if it fails, instead it yields a result
here and I can console log that result to see what's inside there,
so the result in the
then block after comparing. If I now restart my backend server and I log out again and go to login mode, enter
the correct e-mail but the wrong password, you see I get false as a result here.
I don't get an error, I just get false as a result,
so it basically tells me this did not succeed but it throws no error.
So I should simply check if result or if not result, so if this is false
and then I can simply throw an error here and this will then not execute this code but go into the catch
block.
So with that check added in the then block here,
now if I restart my server and I go back to that login page one more time with the correct password
but wrong,
with the correct e-mail but wrong password, I fail.
Now let's try this with the correct password
but wrong e-mail, I fail and now correct e-mail and correct password and I'm forwarded
and now we got this in place too.
So that's a bunch of javascript logic but also some logic where I fetch data from mongodb.
And this is basically it,
let me now conclude this in the next lecture.