WEBVTT

00:03.670 --> 00:05.080
Hey, did everyone had their share?

00:05.080 --> 00:06.580
And in this video we're going to sign up.

00:06.580 --> 00:08.350
A user should be fairly simple.

00:08.560 --> 00:12.340
Let's go ahead and work on with our user controller because that's where we are signing up.

00:12.340 --> 00:17.230
Make sure you have imported the user because now obviously we are going to require it now in the model

00:17.230 --> 00:17.590
itself.

00:17.590 --> 00:23.800
We have designed in such a way that this user there are a couple of compulsory fields that we are actually

00:23.800 --> 00:24.460
banking on.

00:24.460 --> 00:27.310
For example, name is definitely a compulsory one.

00:27.310 --> 00:30.940
Email is definitely compulsory one and a password is also compulsory one.

00:30.940 --> 00:33.460
Now role we are defining it as default photo.

00:33.490 --> 00:36.460
Maybe user don't want to give it right now or maybe he wants to give it.

00:36.460 --> 00:38.680
We'll take care of that in a separate video.

00:38.680 --> 00:40.840
So these are really the basic field up here.

00:41.110 --> 00:45.100
Let's go into the user controller and first let's remove this one up here.

00:45.220 --> 00:50.500
The step one is to grab all of the information which will be coming up into the requested body again,

00:50.500 --> 00:55.180
sending up from the form or maybe the JSON since it's supposed request, I don't have to even think

00:55.180 --> 00:55.840
about it.

00:55.990 --> 01:03.610
So I'm going to go ahead and say, hey, I'm looking for extracting the name and email and password,

01:03.760 --> 01:08.080
so I'm going to D structure that all will be coming up from request or body.

01:08.320 --> 01:08.850
Okay.

01:09.370 --> 01:13.360
Now once this is all done, I need to make sure that at least email is present.

01:13.360 --> 01:14.470
So that is really simple.

01:14.470 --> 01:21.250
I can go ahead and simply say if email is there, if email is not there, then I am a little bit worried

01:21.250 --> 01:25.750
and I need to raise some kind of errors up here now for handling the error parts.

01:25.750 --> 01:28.240
Remember, we created a simple utility for that.

01:28.240 --> 01:29.470
This is a custom error.

01:29.470 --> 01:32.470
So let's bring it up and let me show you how you can use this.

01:32.470 --> 01:35.020
And in case you don't want to use that, then how you can do this.

01:35.020 --> 01:36.700
So let's go ahead and bring that up.

01:37.300 --> 01:43.270
So we're going to be calling this one as what did we call it up here as customer error.

01:43.270 --> 01:46.390
So let's go ahead and say, hey, I want to bring up this custom error.

01:46.390 --> 01:56.950
So custom error and that will be coming up from require and we need to go one directory back into the

01:56.950 --> 01:59.650
utils and we have this one custom error.

01:59.920 --> 02:05.500
Now in case there is no error, then obviously we have to make a kind of a error response to be returned

02:05.500 --> 02:05.800
up here.

02:05.800 --> 02:09.790
So we're going to go ahead and say, hey, I want to return a new response.

02:09.790 --> 02:15.250
And again, make sure since this is kind of a middleware that we have written into this one, this utility

02:15.250 --> 02:20.200
up here, we're going to go ahead and wrap it up into the next part, and we'll be constructing a new

02:20.200 --> 02:20.680
error here.

02:20.680 --> 02:22.810
So let's define a custom error.

02:23.080 --> 02:24.670
Custom error takes two parameter.

02:24.670 --> 02:29.530
The first one is the message, and the second one is in itself the error code itself.

02:29.530 --> 02:37.060
So we'll be saying please send email and the error code would be let just go ahead and give it a 400

02:37.060 --> 02:40.570
because probably you haven't given me the proper information.

02:40.660 --> 02:41.130
Okay.

02:41.860 --> 02:44.290
So this is all this is all what we have so far.

02:44.290 --> 02:48.760
Let's go ahead and save this and see what happens when somebody sends me a post request and doesn't

02:48.760 --> 02:50.560
send me an email in the body itself.

02:50.560 --> 02:52.900
Are we able to process it properly or not?

02:52.930 --> 02:58.150
Let's go back up here into the root and we are not sending anything, so we'll be just making a post

02:58.150 --> 02:58.510
request.

02:58.510 --> 03:02.530
Let's go ahead and send this one and this time it says, Hey, error, please send emails.

03:02.530 --> 03:06.550
So notice here we are grabbing this email in case you want to check raw or something.

03:06.550 --> 03:07.600
Yes, definitely.

03:07.600 --> 03:10.480
It is coming up with a lot more information than this.

03:10.480 --> 03:12.970
We can optimize our error a little bit more.

03:13.000 --> 03:17.920
We will probably work on with that, but definitely we are able to raise the exception properly up here.

03:18.070 --> 03:21.190
Now, what happens if you don't want to use these kinds of customer?

03:21.190 --> 03:22.810
Can I raise the regular errors?

03:22.930 --> 03:25.840
Let me go ahead and show you that you can use this classic error.

03:25.840 --> 03:27.880
It doesn't accept any parameter like this.

03:27.880 --> 03:30.850
It just accepts a simple message that, hey, go ahead and use that.

03:30.880 --> 03:32.560
We have restarted that port.

03:32.560 --> 03:36.100
Let's go ahead and send the again request without sending the email itself.

03:36.100 --> 03:39.310
I go ahead and again notice here it says, please send the email.

03:39.340 --> 03:44.260
So again, depends on what you really want to do and how you want to handle this is absolutely fine.

03:44.260 --> 03:49.540
And it also sends you the server error, which is status code of 400 or 500 internal error.

03:49.600 --> 03:54.310
But in case previously if we worked on with that, which is the customer, I go ahead and save this

03:54.310 --> 03:54.760
one.

03:54.970 --> 03:59.860
I go ahead and send the request again this time also it says status as 500.

03:59.860 --> 04:03.820
Probably we haven't set it up properly, but the error status code should have been changed or we can

04:03.820 --> 04:05.440
send a little bit more information.

04:05.440 --> 04:09.550
So told you this is really a little bit overkill that you want to have.

04:09.550 --> 04:11.500
But again, if you want to have this one.

04:11.800 --> 04:16.540
So notice here we have worked on with this email, so we'll be checking up all the other fields as well

04:16.540 --> 04:22.720
that we want to have a name also and we also want to have a password as well.

04:23.530 --> 04:28.870
So if any of these fields are missing or are not present, then we want to receive a different error

04:28.870 --> 04:30.190
that please send.

04:31.120 --> 04:38.800
Let's write a different way that name, email and password are required.

04:38.800 --> 04:40.120
So any of that is missing.

04:40.120 --> 04:42.850
All of them are required as as kind of a field.

04:43.180 --> 04:43.480
Okay.

04:43.480 --> 04:49.180
Now, once you have verified that these users are kind of required for me, all I have to do is contact

04:49.180 --> 04:51.940
to my MongoDB and send it up there.

04:51.940 --> 04:56.020
Now, right now, we haven't got connected with the MongoDB yet, so we need to figure out that part

04:56.020 --> 04:56.290
as well.

04:56.290 --> 05:01.570
But assuming that we have a connection with MongoDB, eventually that let's go ahead and figure it out.

05:01.920 --> 05:05.760
So let's go ahead and simply go ahead and create the user.

05:05.760 --> 05:08.550
So we will be taking care of this user.

05:08.940 --> 05:15.730
So this user is having a property that says Create and it can just create an object inside the database.

05:15.750 --> 05:18.270
What it's going to take it's going to take name.

05:18.540 --> 05:19.950
It's going to take email.

05:19.950 --> 05:22.050
And it's going to take password.

05:22.470 --> 05:23.760
That is easy and nice.

05:24.030 --> 05:26.690
Now, once it has done that, I want to hold this response.

05:26.700 --> 05:29.940
I'm going to go ahead and say, hey, I want to hold this user just like that.

05:29.940 --> 05:34.170
Since I told you this is really an operation being done on another continent.

05:34.170 --> 05:37.050
That's why we have to go ahead and simply wait for that.

05:37.470 --> 05:42.810
Now, after waiting a little bit, hopefully this has successfully created a user in my back end or

05:42.810 --> 05:43.620
in my database.

05:43.620 --> 05:44.940
I'm holding this user.

05:45.150 --> 05:49.380
Now, the next thing is, obviously, I want to just send some of the cookie values.

05:49.380 --> 05:50.850
Again, it depends on how you want to do.

05:50.880 --> 05:55.060
Maybe you want to send just a message that, hey, a user was created, now go ahead and log in.

05:55.080 --> 05:59.970
In my case, the user has successfully registered or logged in or registered on my website.

05:59.970 --> 06:03.330
I want to send him a cookie token, so let's go ahead and do that now.

06:03.420 --> 06:07.920
So I'll be saying that, hey, I want to grab a token now, so let's go ahead and say, Hey, I want

06:07.920 --> 06:10.740
to grab a token how you are going to get a token.

06:10.740 --> 06:13.260
I told you just like an access user dot email.

06:13.290 --> 06:18.900
Now also I can access all of the, all of the methods that I worked on with that.

06:18.900 --> 06:22.380
Precisely the method that I'm looking for is.

06:23.330 --> 06:27.020
This one, get JWT token, so I'm going to go ahead and run this method.

06:27.020 --> 06:30.560
Copy that and I'll come back here and run this method.

06:31.250 --> 06:33.440
This method will give me a token.

06:33.440 --> 06:37.610
Now I want to set some options and throw these options onto the cookies.

06:37.610 --> 06:38.300
So let's go ahead.

06:38.300 --> 06:43.760
We have already done that in the past, so let's go ahead and set an options that is going to be a simple

06:43.760 --> 06:44.630
object.

06:44.630 --> 06:47.180
The first one is expires.

06:48.170 --> 06:51.320
Expires takes a new date parameter.

06:51.320 --> 06:55.430
And inside the date parameter, we simply have to say date now.

06:55.430 --> 06:59.570
So from current date, how much time do we want to give it for the cookies?

06:59.570 --> 07:02.150
Let's just say we want to give it three days.

07:02.150 --> 07:05.480
So three days.

07:05.510 --> 07:07.850
And then we got to go into.

07:09.300 --> 07:10.350
60.

07:10.600 --> 07:16.360
And then we got to go another 60 and we have to go in 2000.

07:16.380 --> 07:21.660
So I hope you remember this part that this cookies will be active for 24 hours.

07:21.780 --> 07:23.220
So that's what we want to have.

07:23.220 --> 07:24.390
And three days, actually.

07:24.390 --> 07:25.440
So three days.

07:25.440 --> 07:30.180
And you have to go through all the way 24 and many hours and minutes and milliseconds.

07:30.300 --> 07:30.870
Okay.

07:30.900 --> 07:37.440
Now, also, we will be passing on a flag which will say http only.

07:38.070 --> 07:39.960
And that is going to be a true.

07:39.990 --> 07:40.800
There we go.

07:41.010 --> 07:41.550
Okay.

07:41.970 --> 07:46.650
Once these options are set, let's go ahead and send the token in the cookies itself.

07:46.650 --> 07:49.530
So I'm going to be sending a response or resource status.

07:50.670 --> 07:56.610
And this status is going to be simply let's just say 200 is definitely great status in this case and

07:56.610 --> 07:58.650
we'll be sending hey, I want to send a cookie.

07:58.830 --> 08:00.900
The name of the cookie is going to be token.

08:00.930 --> 08:01.680
Feel free to name it.

08:01.680 --> 08:02.790
Anything else?

08:02.910 --> 08:05.010
I want to send this token here.

08:05.010 --> 08:07.170
Just about create a red line number 18.

08:07.320 --> 08:10.140
And after that, I want to send all these options.

08:10.830 --> 08:11.580
There we go.

08:11.640 --> 08:19.290
And also on top of that, I still want to send a JSON response, y JSON response, because maybe this

08:19.290 --> 08:22.750
registration is being done on the mobile, not on the web.

08:22.770 --> 08:28.530
So on the web itself, the cookie will be set up, but maybe in the mobile whatever or whoever is handling

08:28.530 --> 08:33.510
the front end, he wants to take care of this token itself, stored that somewhere, probably in local

08:33.510 --> 08:34.230
storage or wherever.

08:34.230 --> 08:38.740
He likes to store that and then send out the request and attach this token there also.

08:38.760 --> 08:43.410
So I'm going to go ahead and still send a message that success was true in this case.

08:44.070 --> 08:48.900
And you also grab and take the token and maybe this is optional, 100% optional.

08:48.900 --> 08:53.220
We're going to go ahead and send the user itself and we don't have to worry about the password and all

08:53.220 --> 08:53.790
of that.

08:54.930 --> 08:56.610
So this is exactly the same code.

08:56.610 --> 09:02.190
Once you receive the user itself, this user, you can go ahead and just work through all of that.

09:02.310 --> 09:06.960
Now, one thing about this is you can actually rewrite or copy paste this code again and again because

09:06.960 --> 09:12.300
you'll be doing that at the logo, at the login, you'll be doing that at the reset password, you'll

09:12.300 --> 09:13.590
be doing it update password.

09:13.590 --> 09:16.170
So this is a kind of a thing you'll be doing again and again.

09:16.380 --> 09:22.950
So it makes sense that instead of doing this, let's go ahead and create a simple utility method and

09:22.950 --> 09:23.940
just work on there.

09:23.940 --> 09:29.670
So let me go ahead and work on into utils new file and we are going to call this one as simply a cookie

09:29.670 --> 09:33.300
token because that's exactly what it is doing, sending us a cookie token.

09:33.690 --> 09:39.060
And let's go ahead and simply say const cookie token.

09:39.870 --> 09:41.910
And this will be a simple method.

09:41.910 --> 09:44.880
We'll work on the method in a minute, but first lets me export that.

09:44.880 --> 09:52.220
So I'm going to go ahead and save module DOT exports and exporting this cookie token.

09:52.230 --> 09:52.830
There we go.

09:53.970 --> 09:58.700
So how this cookie token is going to work, this is going to be a simple method, just like that.

09:58.710 --> 09:59.670
There we go.

10:00.090 --> 10:02.790
Now, inside this one, we need some parameters.

10:02.790 --> 10:04.260
So let's go ahead and check.

10:04.260 --> 10:06.000
Let's close this one.

10:06.330 --> 10:12.180
And we don't need root as of now, but also we don't need so user controller.

10:12.180 --> 10:16.530
So further down the road I notice what we need to create this token itself.

10:16.530 --> 10:21.480
We definitely need a user since we are sending the response, we need this response as well.

10:21.480 --> 10:22.710
And I guess that's it.

10:22.710 --> 10:23.850
That's all what we need.

10:23.850 --> 10:29.880
So whenever you're using this method, send me a user itself and send me that response so that I can

10:29.880 --> 10:30.960
work on with that.

10:31.560 --> 10:36.990
And now let's go ahead and say that whatever the user you are going to send me, I'll create a token

10:36.990 --> 10:43.710
based on that, and I can just actually cut this entire thing and paste this up here.

10:44.010 --> 10:46.070
Now user will be taken up.

10:46.080 --> 10:51.200
Okay, nice and easy options will be created for me and I'll be sending up this user as well.

10:51.210 --> 10:53.100
I'll be sending the token as well.

10:53.340 --> 10:54.600
And that sounds fantastic.

10:54.600 --> 10:55.770
I can save this one.

10:55.800 --> 11:00.210
I need to now inject this cookie token into my this code itself.

11:01.020 --> 11:04.290
So I'm going to go ahead and say cookie token.

11:04.290 --> 11:09.060
And as soon as I do this, this will auto import the stuff in case yours is not auto importing.

11:09.060 --> 11:10.920
Just go ahead and write this simple line.

11:11.310 --> 11:13.590
And since this is a method, that's it.

11:13.590 --> 11:15.840
That's pretty much it should take.

11:16.440 --> 11:16.680
Okay.

11:16.680 --> 11:20.970
So again, if you want to refactor the code like this, that's up to you in case you don't want you

11:20.970 --> 11:22.050
can actually do that.

11:22.050 --> 11:27.780
The one thing more we can do is we might want to control this cookie time from the EMV as well.

11:27.780 --> 11:29.310
So let's go ahead and do that.

11:29.310 --> 11:36.960
So I will go into my Dot E and V and I'll come here and I'll just say cookie underscore time.

11:37.050 --> 11:41.640
And that would be, let's just say three days for the cookie as well.

11:41.640 --> 11:42.930
Kind of a make sense.

11:42.960 --> 11:45.120
Let's go ahead and copy this.

11:45.690 --> 11:51.600
And inside this one, wherever we have written this three, instead of that, we are going to say process

11:52.140 --> 11:54.660
dot E and V, dot cookie time.

11:54.660 --> 11:58.350
Now again, in case you want to control anything else further down the road, no big deal.

11:58.350 --> 11:59.550
You can always control that.

11:59.550 --> 12:03.080
Just push that thing into the E and V and control it from there.

12:03.150 --> 12:04.710
Really the basic stuff.

12:04.710 --> 12:07.680
I couldn't make this much more easier than what I have done here.

12:07.890 --> 12:08.400
The problem.

12:08.590 --> 12:10.150
Right now is OC.

12:10.240 --> 12:11.160
I forgot this.

12:11.170 --> 12:15.340
The cookie token is not going to work because we need to send the user and the response.

12:16.550 --> 12:21.520
Okay, so this should be working now in theory, but we cannot test it right now because we don't have

12:21.520 --> 12:22.830
any connection with the database.

12:22.840 --> 12:23.710
Nothing is working.

12:23.710 --> 12:27.700
This is just a method we have written, but actually it should be decent.

12:27.700 --> 12:27.970
Fine.

12:27.970 --> 12:32.350
Here in the next video we're going to go ahead and connect the database connection at the very start

12:32.350 --> 12:36.780
of our app and then we'll check out that whether this much of the code is working or not.

12:36.790 --> 12:38.680
Let's go ahead and catch up in the next video.
