Hey, did everyone had their share?
And in this video we're going to sign up.
A user should be fairly simple.
Let's go ahead and work on with our user controller because that's where we are signing up.
Make sure you have imported the user because now obviously we are going to require it now in the model
itself.
We have designed in such a way that this user there are a couple of compulsory fields that we are actually
banking on.
For example, name is definitely a compulsory one.
Email is definitely compulsory one and a password is also compulsory one.
Now role we are defining it as default photo.
Maybe user don't want to give it right now or maybe he wants to give it.
We'll take care of that in a separate video.
So these are really the basic field up here.
Let's go into the user controller and first let's remove this one up here.
The step one is to grab all of the information which will be coming up into the requested body again,
sending up from the form or maybe the JSON since it's supposed request, I don't have to even think
about it.
So I'm going to go ahead and say, hey, I'm looking for extracting the name and email and password,
so I'm going to D structure that all will be coming up from request or body.
Okay.
Now once this is all done, I need to make sure that at least email is present.
So that is really simple.
I can go ahead and simply say if email is there, if email is not there, then I am a little bit worried
and I need to raise some kind of errors up here now for handling the error parts.
Remember, we created a simple utility for that.
This is a custom error.
So let's bring it up and let me show you how you can use this.
And in case you don't want to use that, then how you can do this.
So let's go ahead and bring that up.
So we're going to be calling this one as what did we call it up here as customer error.
So let's go ahead and say, hey, I want to bring up this custom error.
So custom error and that will be coming up from require and we need to go one directory back into the
utils and we have this one custom error.
Now in case there is no error, then obviously we have to make a kind of a error response to be returned
up here.
So we're going to go ahead and say, hey, I want to return a new response.
And again, make sure since this is kind of a middleware that we have written into this one, this utility
up here, we're going to go ahead and wrap it up into the next part, and we'll be constructing a new
error here.
So let's define a custom error.
Custom error takes two parameter.
The first one is the message, and the second one is in itself the error code itself.
So we'll be saying please send email and the error code would be let just go ahead and give it a 400
because probably you haven't given me the proper information.
Okay.
So this is all this is all what we have so far.
Let's go ahead and save this and see what happens when somebody sends me a post request and doesn't
send me an email in the body itself.
Are we able to process it properly or not?
Let's go back up here into the root and we are not sending anything, so we'll be just making a post
request.
Let's go ahead and send this one and this time it says, Hey, error, please send emails.
So notice here we are grabbing this email in case you want to check raw or something.
Yes, definitely.
It is coming up with a lot more information than this.
We can optimize our error a little bit more.
We will probably work on with that, but definitely we are able to raise the exception properly up here.
Now, what happens if you don't want to use these kinds of customer?
Can I raise the regular errors?
Let me go ahead and show you that you can use this classic error.
It doesn't accept any parameter like this.
It just accepts a simple message that, hey, go ahead and use that.
We have restarted that port.
Let's go ahead and send the again request without sending the email itself.
I go ahead and again notice here it says, please send the email.
So again, depends on what you really want to do and how you want to handle this is absolutely fine.
And it also sends you the server error, which is status code of 400 or 500 internal error.
But in case previously if we worked on with that, which is the customer, I go ahead and save this
one.
I go ahead and send the request again this time also it says status as 500.
Probably we haven't set it up properly, but the error status code should have been changed or we can
send a little bit more information.
So told you this is really a little bit overkill that you want to have.
But again, if you want to have this one.
So notice here we have worked on with this email, so we'll be checking up all the other fields as well
that we want to have a name also and we also want to have a password as well.
So if any of these fields are missing or are not present, then we want to receive a different error
that please send.
Let's write a different way that name, email and password are required.
So any of that is missing.
All of them are required as as kind of a field.
Okay.
Now, once you have verified that these users are kind of required for me, all I have to do is contact
to my MongoDB and send it up there.
Now, right now, we haven't got connected with the MongoDB yet, so we need to figure out that part
as well.
But assuming that we have a connection with MongoDB, eventually that let's go ahead and figure it out.
So let's go ahead and simply go ahead and create the user.
So we will be taking care of this user.
So this user is having a property that says Create and it can just create an object inside the database.
What it's going to take it's going to take name.
It's going to take email.
And it's going to take password.
That is easy and nice.
Now, once it has done that, I want to hold this response.
I'm going to go ahead and say, hey, I want to hold this user just like that.
Since I told you this is really an operation being done on another continent.
That's why we have to go ahead and simply wait for that.
Now, after waiting a little bit, hopefully this has successfully created a user in my back end or
in my database.
I'm holding this user.
Now, the next thing is, obviously, I want to just send some of the cookie values.
Again, it depends on how you want to do.
Maybe you want to send just a message that, hey, a user was created, now go ahead and log in.
In my case, the user has successfully registered or logged in or registered on my website.
I want to send him a cookie token, so let's go ahead and do that now.
So I'll be saying that, hey, I want to grab a token now, so let's go ahead and say, Hey, I want
to grab a token how you are going to get a token.
I told you just like an access user dot email.
Now also I can access all of the, all of the methods that I worked on with that.
Precisely the method that I'm looking for is.
This one, get JWT token, so I'm going to go ahead and run this method.
Copy that and I'll come back here and run this method.
This method will give me a token.
Now I want to set some options and throw these options onto the cookies.
So let's go ahead.
We have already done that in the past, so let's go ahead and set an options that is going to be a simple
object.
The first one is expires.
Expires takes a new date parameter.
And inside the date parameter, we simply have to say date now.
So from current date, how much time do we want to give it for the cookies?
Let's just say we want to give it three days.
So three days.
And then we got to go into.
60.
And then we got to go another 60 and we have to go in 2000.
So I hope you remember this part that this cookies will be active for 24 hours.
So that's what we want to have.
And three days, actually.
So three days.
And you have to go through all the way 24 and many hours and minutes and milliseconds.
Okay.
Now, also, we will be passing on a flag which will say http only.
And that is going to be a true.
There we go.
Okay.
Once these options are set, let's go ahead and send the token in the cookies itself.
So I'm going to be sending a response or resource status.
And this status is going to be simply let's just say 200 is definitely great status in this case and
we'll be sending hey, I want to send a cookie.
The name of the cookie is going to be token.
Feel free to name it.
Anything else?
I want to send this token here.
Just about create a red line number 18.
And after that, I want to send all these options.
There we go.
And also on top of that, I still want to send a JSON response, y JSON response, because maybe this
registration is being done on the mobile, not on the web.
So on the web itself, the cookie will be set up, but maybe in the mobile whatever or whoever is handling
the front end, he wants to take care of this token itself, stored that somewhere, probably in local
storage or wherever.
He likes to store that and then send out the request and attach this token there also.
So I'm going to go ahead and still send a message that success was true in this case.
And you also grab and take the token and maybe this is optional, 100% optional.
We're going to go ahead and send the user itself and we don't have to worry about the password and all
of that.
So this is exactly the same code.
Once you receive the user itself, this user, you can go ahead and just work through all of that.
Now, one thing about this is you can actually rewrite or copy paste this code again and again because
you'll be doing that at the logo, at the login, you'll be doing that at the reset password, you'll
be doing it update password.
So this is a kind of a thing you'll be doing again and again.
So it makes sense that instead of doing this, let's go ahead and create a simple utility method and
just work on there.
So let me go ahead and work on into utils new file and we are going to call this one as simply a cookie
token because that's exactly what it is doing, sending us a cookie token.
And let's go ahead and simply say const cookie token.
And this will be a simple method.
We'll work on the method in a minute, but first lets me export that.
So I'm going to go ahead and save module DOT exports and exporting this cookie token.
There we go.
So how this cookie token is going to work, this is going to be a simple method, just like that.
There we go.
Now, inside this one, we need some parameters.
So let's go ahead and check.
Let's close this one.
And we don't need root as of now, but also we don't need so user controller.
So further down the road I notice what we need to create this token itself.
We definitely need a user since we are sending the response, we need this response as well.
And I guess that's it.
That's all what we need.
So whenever you're using this method, send me a user itself and send me that response so that I can
work on with that.
And now let's go ahead and say that whatever the user you are going to send me, I'll create a token
based on that, and I can just actually cut this entire thing and paste this up here.
Now user will be taken up.
Okay, nice and easy options will be created for me and I'll be sending up this user as well.
I'll be sending the token as well.
And that sounds fantastic.
I can save this one.
I need to now inject this cookie token into my this code itself.
So I'm going to go ahead and say cookie token.
And as soon as I do this, this will auto import the stuff in case yours is not auto importing.
Just go ahead and write this simple line.
And since this is a method, that's it.
That's pretty much it should take.
Okay.
So again, if you want to refactor the code like this, that's up to you in case you don't want you
can actually do that.
The one thing more we can do is we might want to control this cookie time from the EMV as well.
So let's go ahead and do that.
So I will go into my Dot E and V and I'll come here and I'll just say cookie underscore time.
And that would be, let's just say three days for the cookie as well.
Kind of a make sense.
Let's go ahead and copy this.
And inside this one, wherever we have written this three, instead of that, we are going to say process
dot E and V, dot cookie time.
Now again, in case you want to control anything else further down the road, no big deal.
You can always control that.
Just push that thing into the E and V and control it from there.
Really the basic stuff.
I couldn't make this much more easier than what I have done here.
The problem.
Right now is OC.
I forgot this.
The cookie token is not going to work because we need to send the user and the response.
Okay, so this should be working now in theory, but we cannot test it right now because we don't have
any connection with the database.
Nothing is working.
This is just a method we have written, but actually it should be decent.
Fine.
Here in the next video we're going to go ahead and connect the database connection at the very start
of our app and then we'll check out that whether this much of the code is working or not.
Let's go ahead and catch up in the next video.