- [Instructor] Okay, so the next function
that we need to create is the function
for registering new users.
So let's go ahead and keep working on this.
So let's do an export
of a function which we'll call register.
And then this function takes two arguments
so a request and a response.
And then inside of that function,
we'll create first a variable
that we'll call newUser.
And this function will pass a new user,
so remember, we've created a variable here
which basically adds the model user
and passes the schema that we created earlier
and we're going to use this to create a new user.
Like so.
Leveraging what we've created up there.
And we'll pass from the request what is in the body.
So remember, when we make transactions with Postman here,
in the body, we're passing the data to create a new user
and this is what will be in the body of the request
which is here.
So now we'll have the information for the new user
and now we need to create the new user
and we'll use the hashPassword
of that newUser so remember in the body,
there's going to be a password.
So we'll use that and encrypt with bcrypt.password
before we pass it to the database
and that's why we need to in the model here
to compare passwords
which are encrypted
but bcrypt will decrypt those passwords
and then compare them.
But we still need to pass a password
that is encrypted first inside of our database.
So we'll take the request.body.password
and again, I've called this password.
So if it's a little bit confusing to you
in between hashPassword and password,
remember that the request is going
to send a password but in our database,
it's called hashPassword like so.
And then we'll pass the value 10.
So this is the number that hashSync requires to encrypt
so this is the algorithm.
And then once we have this,
we can save it to the database.
So we're going to save it
and first and this is the typical code
that you would use in an API to send new stuff to Mongo.
So we're going to save it.
And I just want to fix this right away
'cause this goes outside of the curly braces
just to be absolutely certain.
So if there's an error,
when we are saving inside of the database,
return a response with the status of 400.
And then send
the message error
so let me know what is the error if there is one.
Else what you'll do is return the user
that we just saved.
So what we'll do is first,
omit to add the password from the response
because we don't want to pass back the password
to the response,
especially in any response you don't want to send
a password back to the user or the front end
because that could be used for hacking purpose
and so on and so forth.
So we are basically setting as undefined
when we're passing back to the response
and then we're returning a response with a JSON message
which includes the user and again, the user password
won't be set as the original request.
Okay.
So let's save this.
Okay, so now this was the function to register a new user.
Also using best practices for password.
Again using best practices for security measures.
Let's move on.