- [Instructor] Okay, so now what we're going to do is
start writing code related to bcrypt.
If you're not familiar with bcrypt,
it's a package that allows you to encrypt
and decrypt data, so like a password.
So what we're going to do first is install bcrypt
and while we're actually installing new packages,
we'll also install jsonwebtoken.
So let's go into NPM I, or you can do install
bcrypt
and then jsonwebtoken.
We'll use jsonwebtoken later on,
but I wanted to install it all on this one pass.
So let's go ahead and hit enter.
Perfect, so everything is installed.
So we can close the terminal for now.
And if you want to check, you can take a look at
the package.json file, and you're going to see
bcrypt installed and jsonwebtoken, so we're ready to go.
So now what we need to do is import bcrypt,
so let's go ahead and import bcrypt
from
bcrypt, like so.
And by the way, when you have a library
that you want to import specific functions or exports
from that library, what you could do is
do something like that, so import from
the library.
And then do the curly braces here
when you're importing something specific,
and then start typing, so for example,
compare, comparesync, or any specific functions
that you'd like to use inside of that library.
But in this case, I want to use the whole library,
so I'm just going to go for this particular item here.
So now what we need to do is create the function
that will compare the password that has been sent
from the request to the one that we have in the database.
So bcrypt will decrypt those two passwords,
compare them, and then say, "yeah, it's the same password."
So this is the function that we're going to create now.
So I'm going to leverage user schema
and add a method to user schema,
and I'm going to call this compare password,
which will have, or take as argument,
the password that is coming from the request,
and then the password that we have in our database,
which is the hash password.
And then we'll create a function.
And in this function, we're going to return
from bcrypt's function called comparesync.
And we're going to compare the password again
that we have from the arguments in our request here,
and also the hash password.
So this function will compare them both,
make sure that they are decrypted while they're comparing,
and if they're both matching,
then we have a user that can login.
Perfect.
And while I'm at it, let's just make sure that
we have methods, and not method.
Let's make sure that everything is okay.
Bcrypt, bcrypt, so our entire code is all good.
So you can save and move onto the next movie.