All right, guys.
So now we want to be able to register a user and we already have the route.
We already have the controller function.
So now we just need to add our code in here.
So first thing we're going to do is get from the request body.
We need the name, email and password.
That's what's going to be sent.
So let's say we want to destructure from request body, the name, email and password.
Okay, so we'll get that.
Then we want to check to see if the user exists.
So I'm going to say const user exists and then we'll set that to await and we're going to use the user
model and then find one.
And we'll pass in here the email because that's what we're searching for.
Okay.
So if the user exists and they're trying to register them, we want to just respond with an error that
says user already exists.
So let's go ahead and do that.
So what we're saying here is if the user exists, respond with a 400, which means a client error,
meaning they did something wrong, right?
They're trying to register a user that's already there.
And then we're just throwing a new error saying user already exists.
So underneath that, if the user doesn't exist, then we want to create it.
So we'll say user equals await, user create.
And inside that we're going to pass in the name, the email and the password that comes from the body,
which ultimately is going to come from a form from our user interface.
So that will create the user.
Now down here, we're just going to check for the user and if there is a user, then we're going to
respond with a status of 201, which means everything's good and something was created, the ID, the
name, email and is admin.
Same stuff.
We return after they're authenticated.
And then in the else we're just going to set a 400 and then throw an error that says invalid user data.
All right.
Now we have to obviously we have to hash the password because right now the way we have it, it's going
to take the plain text password from the body and it's going to add that to the database.
So there's a few ways we could handle it.
We could do it here, but again, I like to keep the controller methods or controller functions as slim
as possible.
So we're going to do the encryption in the model and the user model.
So let's open that up just like we did the match password here in the model.
So we already brought Bcrypt in.
And what I'm going to do is add on user schema.
Am I going to do methods?
I'm going to do dot pre because what that does is it allows us to do something before it's saved in
the database.
Okay.
So anything that's that has to do with the user.
If we're saving something to the database, if we use pre, then this will run before because obviously
we want to hash the password before it gets saved to the database.
You can also do post which would do something after, but obviously we need to do this before.
And there's different actions that you can do before or whatever.
In this case we want to do before save.
So we pass in.
Save.
All right, Then we're going to go into the function body and we're going to say if.
Let's say.
If not, this dot is modified.
Okay, So what this is doing here, if this is not modified password, then we're just going to call
the next piece of middleware because that's what this is, is middleware for Mongoose.
So if we're just saving some user data, but we're not dealing with the password, then it's just going
to move on.
It's just going to next.
All right.
So outside of that, though, if we are modifying the password, then we're first going to generate
a salt with the bcrypt method of gin salt, and then we're going to set this dot password, this pertaining
to the current user that we're saving.
And then we're going to get the password field.
And instead of setting it to just the plain text, we're going to set it to a hashed password using
the hash method from the Bcrypt package, which takes in the password and then the salt, and then it
will create a hash and it'll get saved to the database because now we're we're trading basically trading
this password that's plain text that's passed in from the form.
We're trading it for this hashed password before it's saved pre save.
So hopefully that makes sense.
So let's make sure that this file is saved.
And what we want to do once that user is registered is we want to authenticate, basically, we want
to create our Json web token and we want to create our Http only cookie, just like we did above, just
like we did right here.
Now.
Instead of just putting the same code because I would do the same exact thing, I'd put the user ID
into a Json web token and then save it to a cookie instead of repeating ourselves.
Let's create a separate file for this or a separate function for this.
So let's go into let's see, do we have a utils here?
Or that that was in the front end right where we had our.
Yeah, we have a front end utils, but let's go into the back end and let's create a new folder called
utils.
And in this utils let's create a file called generate token dot js.
All right.
And then in this file, basically we're going to do this.
So we need to bring in import Json web token into this file.
And then let's create a function.
We'll say const, generate.
Generate token.
And it's going to take in two things, actually.
It's going to take in the response because we are going to need to use resort cookie and then it's also
going to take in the user ID, let's call it user ID like that.
Okay.
And then inside the function body here, I'm going to grab what we have in the auth.
So where we're defining the token and where we're setting the cookie, I'm going to just cut that.
Okay.
And then in generate token, we'll go ahead and paste that in.
And now let's see, instead of setting user ID to this, we want to set it to the ID that's being passed
in, which is the same name as this.
So we can actually just get rid of that.
That should work.
And let's see down here, we're setting JWT.
I don't think we need to change anything here.
Yeah, that should be good.
Now we just want to export, so we're going to export as default generate token.
So now what we can do is in our user controller, we can bring this in.
So right here.
And we're not going to need to bring in JWT anymore because we're not using it here.
So let's instead import generate token from our utility file.
And then to use it, we're just going to go where we just cut that code from right above the this res.json
and we're going to call generate token.
We're going to pass in the response and we're going to pass in the user ID, Okay?
And then here we're just going to respond the same way we did.
Now what we can do is use this generate token down in the register as well.
So we want to put this.
Uh, right after here, right after we check for the user.
Right before the response.
So now we're not repeating ourselves.
We're sticking to the dry principle.
And instead of having all this code twice, we have a single function that we can call in both places.
And now we can test the register.
So let's come over here, let's go to register and we're going to go into the body.
And let's add a name.
So I'm just going to.
Put my own name and then an email.
I'll just say Brad at email and then a password.
I'll just do one, two, three, four, five, six.
And now let's send.
And you can see we get back our user, our user was created.
It was given an ID, and now we have our cookie set.
So I should be able to go to my get user profile, which is a protected route and send.
And that works because I'm logged in because I just registered.
Okay.
Now, if I go to log out and I send we get logged out successfully.
If I try to go back to get my profile, I can't because I'm not I'm no longer logged in, but I am registered.
And you can also check if you want.
You can check through Compass.
I just need to reload.
The data here.
And now you can see Brad Travis is now a user.
And my password, of course, is hashed.
Okay.
So, so far, so good.
Now, the next in the next video, I want to actually make these profile endpoints work.
So I want to be able to get the user profile.
I also want to be able to update the user profile if we want to change, you know, change some user
data.