WEBVTT

00:03.760 --> 00:05.260
Hey, did everyone had a shower?

00:05.260 --> 00:07.240
And welcome to another new section.

00:07.270 --> 00:12.700
Now, in this section, we are going to focus purely on the users and will create some models of it,

00:12.700 --> 00:18.370
some of the pre functions for it, some hooks for it, controllers routes and all about the users that

00:18.370 --> 00:19.510
we need to do.

00:19.540 --> 00:21.190
Let's go ahead and get started with that.

00:21.190 --> 00:23.470
For that, obviously, we'll go into the models first.

00:23.470 --> 00:25.900
So let's go up there and inside the model.

00:25.900 --> 00:27.490
We need to create a simple file.

00:27.490 --> 00:29.020
So we are calling it as models.

00:29.380 --> 00:34.000
Let's right click and click on the new file and this one is going to be simple user.

00:34.000 --> 00:37.120
So call it with the uppercase or lowercase, doesn't really matter.

00:37.330 --> 00:40.540
It's your personal preference, but make sure you are consistent around that.

00:40.900 --> 00:45.190
And once we are done with that, this is the section where we are going to also learn a bit about the

00:45.190 --> 00:47.380
validator and its use case as well.

00:47.560 --> 00:52.900
So first, let's go ahead and bring in the mongoose because that is going to be the main structure that

00:52.900 --> 00:54.820
will help us to do almost everything.

00:54.910 --> 01:00.280
So we're going to say, Hey, Mongoose, please come in by saying, Hey, Mongoose, just require the

01:00.280 --> 01:01.060
mongoose.

01:01.570 --> 01:02.410
There we go.

01:02.410 --> 01:05.980
Since we'll be using the validator, it's good that we bring it up here.

01:05.980 --> 01:07.390
I'll show you how to use that.

01:07.390 --> 01:08.350
It's really, really simple.

01:08.350 --> 01:14.290
Once you see that at one time, it is going to be super easy for you after after every single time after

01:14.290 --> 01:14.740
that.

01:14.860 --> 01:22.180
So Validator is going to be coming up from require validator.

01:22.180 --> 01:22.730
Okay.

01:23.350 --> 01:24.430
Now how do we do that?

01:24.430 --> 01:26.500
The basic we have already seen it one time.

01:26.500 --> 01:28.510
So this is going to be a nice revision for you.

01:28.510 --> 01:30.760
So let's call this one as user schema.

01:30.880 --> 01:35.320
This is kind of a good practice that whatever you are naming your file, just add a schema after that.

01:35.320 --> 01:38.500
And at the very end of it, import time, we just properly name that.

01:38.500 --> 01:39.550
So that's how we do it.

01:39.760 --> 01:44.920
And we are going to go ahead and say, Hey, Mongoose, give me a method that says a schema.

01:45.160 --> 01:47.560
And in the schema we go ahead and write all the schema.

01:47.560 --> 01:49.090
So this is the basics of it.

01:49.120 --> 01:52.480
Now once you are done with that, what are the things that you need to write after that?

01:52.480 --> 01:55.690
At the very end of it, we always go ahead and export the schema.

01:55.840 --> 01:58.960
So we're going to say module dot x sports.

01:59.260 --> 02:04.750
That is going to be equal to mongoose dot model.

02:04.960 --> 02:08.140
So Mongoose will take this schema and convert it into a model.

02:08.140 --> 02:09.640
So how is it going to do it?

02:09.670 --> 02:12.220
We usually write this as uppercase user.

02:12.520 --> 02:14.530
This is just our own personal preferences.

02:14.530 --> 02:19.420
Mongoose doesn't actually follow too much there, and we have to provide what schema to follow for this

02:19.420 --> 02:21.580
particular model, which is user schema.

02:21.820 --> 02:27.400
Now again mentioning this, although we have discussed this quite a lot in the past as well, that Mongoose

02:27.400 --> 02:32.410
will actually take this user will convert it into a lowercase that will be user and in the database

02:32.410 --> 02:34.450
itself it will add it as users.

02:34.450 --> 02:37.600
So whatever the plural, it is able to find it in its own dictionary.

02:37.600 --> 02:40.000
It will able to it will just do that pretty simple.

02:40.000 --> 02:42.790
Nothing much more that we haven't seen already.

02:42.940 --> 02:47.230
Now, after that, we need to provide all these properties, so we're going to go ahead and get started

02:47.230 --> 02:47.710
with name.

02:47.710 --> 02:53.740
We already had a long discussion about name, email, password, photo role, forgot password token,

02:53.740 --> 02:56.290
forgot password expiry and of course created add.

02:56.290 --> 02:58.450
So we just need to provide all of that.

02:58.450 --> 03:02.740
Now coming on to that, you can go ahead and say name string and that's it.

03:03.040 --> 03:06.280
This is a perfectly valid, 100% valid schema.

03:06.280 --> 03:11.260
This will add a field into the mongoose that, hey, whenever the name comes in it is going to come

03:11.260 --> 03:12.640
up as a part of a string.

03:12.790 --> 03:14.770
Now this is okay, this is totally fine.

03:14.770 --> 03:16.750
In fact, for some of the cases we are going to do it.

03:16.750 --> 03:20.920
But in this case, the name is a little bit more complex and requires a bit more field.

03:20.920 --> 03:25.510
So in that case, we provide an object just like this, and we go ahead and say, Hey, the type is

03:25.510 --> 03:26.440
going to be string.

03:26.440 --> 03:28.900
What is the difference between this and the first one?

03:28.990 --> 03:29.980
Nothing at all.

03:30.050 --> 03:30.460
Nothing.

03:30.460 --> 03:31.300
No difference at all.

03:31.300 --> 03:34.480
This is another way kind of another constructor that you can use.

03:34.630 --> 03:39.340
But the reason why we are providing an object, because we need to provide more fields so we can actually

03:39.340 --> 03:43.630
mark any field as required as true or anything similar to that.

03:43.750 --> 03:46.690
So you can go ahead and just say required as true.

03:46.690 --> 03:49.150
This is again totally 100% fine.

03:49.150 --> 03:55.150
But if you just go ahead and pass on an error here and say that first property is true, the second

03:55.150 --> 03:56.560
property is the error message.

03:56.560 --> 04:01.930
When somebody doesn't pass me this error message, I'm automatically going to provide an error message

04:01.930 --> 04:02.140
here.

04:02.140 --> 04:07.570
So what that error message is going to be, please provide a name.

04:07.900 --> 04:12.760
So in this way, I'm not only just providing that what fields are required, but I'm also providing

04:12.760 --> 04:16.420
that what error message should go up when this field is not being passed to us?

04:16.510 --> 04:21.340
This is one of the thing where mongo is actually shine a lot that you don't have to craft some different

04:21.340 --> 04:25.300
messages and keep that in to file that, hey, what happens when name is not parsed and all of that?

04:25.480 --> 04:30.190
Everything is just provided right into the models, which I think is a great way of getting started.

04:30.610 --> 04:35.920
After that, we can also go ahead and provide that what could be the max length of the name.

04:35.920 --> 04:38.200
Now again, if you want to provide, go ahead, provide.

04:38.200 --> 04:41.050
If you don't want to go ahead and provide that, it's up to you.

04:41.170 --> 04:46.420
I'll say that 40 characters are more than enough for a name, but maybe there are some names which can

04:46.420 --> 04:47.620
be more than 40.

04:47.620 --> 04:51.580
But we are expecting that, hey, somebody needs to acronym that name and all of that.

04:51.580 --> 05:00.490
So we're going to go ahead and provide error message that name should be under.

05:03.720 --> 05:04.850
Characters.

05:05.790 --> 05:07.620
Hopefully I wrote everything correctly.

05:07.710 --> 05:10.080
Okay, so this is the basics of how you define that.

05:10.110 --> 05:11.730
Do we have more properties than this?

05:11.760 --> 05:12.850
Yes, of course you'll read.

05:12.870 --> 05:15.450
You have to read the Mongoose documentation for that.

05:15.450 --> 05:17.700
But there is a whole lot of that.

05:17.730 --> 05:22.080
Now, next, we are going to use this validator because we could have used the validator inside the

05:22.080 --> 05:22.660
name itself.

05:22.680 --> 05:25.230
But let's go ahead and use that onto the next field, which is email.

05:25.230 --> 05:27.300
So I'll copy this and paste it up here.

05:27.570 --> 05:29.730
Now, this time, this field is email.

05:30.030 --> 05:34.680
Again, if I can write that email now this is going to be a type of string.

05:34.680 --> 05:37.350
The required is true and we're going to go ahead and change that.

05:37.350 --> 05:39.090
That please provide an email.

05:39.690 --> 05:41.460
So that is going to be better.

05:41.490 --> 05:44.550
Now, this time we don't have any maxlength or something.

05:44.550 --> 05:46.530
We are going to be using validator in this case.

05:46.530 --> 05:48.900
So let's go ahead and use validator for that.

05:49.080 --> 05:53.920
So we actually have a property in the mongoose itself, which is validate.

05:53.940 --> 05:58.410
Now this is where Validator actually shines and you can write your own validators as well.

05:58.410 --> 05:59.220
There is no big issue.

05:59.220 --> 06:02.100
They are pretty fairly easy to write there as well.

06:02.190 --> 06:06.870
So this is where I'm going to go ahead and say, hey, validator, which we brought in from Validator

06:06.870 --> 06:11.110
and as soon as I put a dot, you can see there is a whole lot of things that it can do for you.

06:11.130 --> 06:14.070
It can check a whole lot of user schema and all of that.

06:14.070 --> 06:21.270
But if I go ahead and say is email notice, we just saw in the documentation in earlier videos that

06:21.270 --> 06:24.870
it can check for the data you are by email and a whole bunch of other things.

06:24.870 --> 06:26.430
This is exactly how you do that.

06:26.700 --> 06:36.690
And after that, you can provide an error message that please enter email in correct format.

06:39.200 --> 06:39.820
There we go.

06:39.830 --> 06:41.570
So now this is going to be pretty basic.

06:41.570 --> 06:44.060
That email needs to be in the correct format as well.

06:44.120 --> 06:49.040
Now, one more thing, which is really fantastic and really good about the mangoes that it gives us.

06:49.040 --> 06:52.580
There is a field unique and I can turn this on as true.

06:52.730 --> 06:58.970
Now Mongoose will automatically look into the database before saving it that whether this email already

06:58.970 --> 07:03.850
exists in my database or not, although I will check it onto the my back end part itself.

07:03.860 --> 07:08.960
But it's really great to have another step of protection that I'm getting all the unique emails while

07:08.960 --> 07:11.300
signing up the user or making it fresh record.

07:11.300 --> 07:14.300
So that is always a fantastic thing that comes in.

07:14.870 --> 07:19.490
Now let's go ahead and after email, we're going to go ahead and have another field, which is going

07:19.490 --> 07:20.240
to be password.

07:20.240 --> 07:21.500
So let's go up here.

07:22.040 --> 07:24.050
And yes, I duplicated just that.

07:25.340 --> 07:27.470
So this one is going to be password.

07:27.470 --> 07:32.240
And remember, we never, ever, ever save the password, confirm field in the database no matter what

07:32.240 --> 07:33.470
language you are writing in.

07:33.500 --> 07:35.600
This is how we go work with that.

07:35.610 --> 07:44.540
So again, required is true and we are going to say please provide a password as we definitely can use

07:44.540 --> 07:48.370
kind of a validate to check whether somebody is providing a complex and all of that.

07:48.380 --> 07:49.610
I wouldn't be doing that.

07:49.620 --> 07:51.260
You can actually go ahead and do that.

07:51.260 --> 07:52.670
I wouldn't be providing that.

07:52.760 --> 07:53.390
Definitely.

07:53.390 --> 07:56.570
We don't need unique, so we're going to go ahead and remove that.

07:57.020 --> 08:00.350
We can actually go ahead and say that it should be a minimum length.

08:00.350 --> 08:05.030
So for protection sake, you can go ahead and say, hey, there should be at least six, seven or ten,

08:05.030 --> 08:06.980
whatever the characters you are looking for.

08:07.190 --> 08:18.020
So we will say that similar to this, we'll say password should be at least six cat.

08:18.950 --> 08:23.450
I'm going to make it short character for care from now onwards.

08:23.730 --> 08:28.490
Okay, one more thing, which is really, really important and it depends on how you are flowing actually

08:28.490 --> 08:29.480
your application.

08:29.480 --> 08:35.180
We saw that when once we were doing these authentication, all of that, that our password was also

08:35.180 --> 08:37.550
going whenever we we were bringing the user.

08:37.550 --> 08:43.180
So we had to just say that a user dot password equals to undefined and all of that because password

08:43.200 --> 08:46.940
was coming in as default whenever we were bringing the user model itself.

08:47.000 --> 08:51.320
Now what you can do is you can provide a field here which says select false.

08:51.410 --> 08:56.930
So now what will happen is whenever you are going to select any model or any particular user and we'll

08:56.930 --> 09:00.230
try to bring in the password field will not come there.

09:00.230 --> 09:04.790
In case you want to have the password field, you have to explicitly mention that that, hey, this

09:04.790 --> 09:08.780
time I'm changing some password or comparing the password or something.

09:08.780 --> 09:10.010
Please give me that password.

09:10.010 --> 09:15.230
Field So this is kind of a flow again, some people like to just put it undefined at that time.

09:15.230 --> 09:18.920
Some people like to put it here as select false depends on the flow.

09:18.920 --> 09:20.660
And again, all of them are correct.

09:20.660 --> 09:24.290
There is no such thing as bad or good or something like that.

09:25.190 --> 09:27.830
Okay, let's go ahead and select this.

09:28.790 --> 09:31.880
And after that, we are going to go ahead and bring in the role.

09:31.880 --> 09:37.010
So we discussed that there is going to be a role of every single user and we don't need this much of

09:37.010 --> 09:38.210
data for the role.

09:38.210 --> 09:40.460
We can actually go ahead and remove this.

09:40.460 --> 09:46.130
So the role is going to be type of string and we need to provide a default for the role as well because

09:46.130 --> 09:50.510
as soon as the user signs up, we need to provide him some role because that's how our functionality

09:50.510 --> 09:51.590
is going to work on.

09:51.680 --> 09:56.900
We'll say that the default is user, but we will provide a functionality later on to the admin that

09:56.900 --> 10:02.270
hey, this is how you can go ahead and make this user as admin and user whatever you like.

10:02.300 --> 10:08.570
Again, the very first user who is going to be admin, we cannot make him admin just like anything.

10:08.570 --> 10:14.120
We have to go into the database, change the field manually via through MongoDB, Command Shell or via

10:14.120 --> 10:15.740
their UI interface.

10:15.740 --> 10:21.830
However, the first user needs to be made admin explicitly and you have to mention that part.

10:22.100 --> 10:22.380
Okay.

10:22.400 --> 10:23.930
So this is the basics of it.

10:24.020 --> 10:25.010
What else we are missing?

10:25.010 --> 10:28.490
Let's check that we have got name, email, password.

10:28.490 --> 10:29.570
We are missing photo.

10:29.600 --> 10:33.770
We have got through the role so we need to check out the photo that how we are going to do with that.

10:34.160 --> 10:34.460
Okay.

10:34.460 --> 10:40.070
Let's go ahead and make a copy of this and call this one as simply photo.

10:40.280 --> 10:41.180
Photo.

10:41.210 --> 10:41.960
There we go.

10:43.090 --> 10:43.340
Okay.

10:43.450 --> 10:48.940
So this photo is a little bit different because inside this photo we obviously have an object.

10:48.940 --> 10:54.040
This is not an airfield field, so this is fine, but we are containing two fields inside the photo.

10:54.070 --> 10:57.480
The first is ID itself, which we'll get back from the cloud entry.

10:57.490 --> 11:00.340
Second is the secure URL, which we started at the time.

11:00.340 --> 11:02.190
We were uploading a whole lot of photos.

11:02.230 --> 11:04.330
So let's go ahead and call this one as ID.

11:04.360 --> 11:08.140
So ID further is going to be done as a property.

11:08.320 --> 11:13.510
If it is just one for something like this string, you can go ahead and do and you can just get away

11:13.510 --> 11:15.000
with that, something like this.

11:15.010 --> 11:16.900
And again, same thing goes here.

11:16.900 --> 11:19.330
You can go ahead and provide this field something like this.

11:19.480 --> 11:24.640
So I'm going to mention that this is of type string and this is a required field.

11:25.800 --> 11:28.560
So required is going to be true.

11:28.770 --> 11:29.490
There we go.

11:29.640 --> 11:34.050
Now, same thing will happen to the secure security URL.

11:34.050 --> 11:36.570
So let's go ahead and make a copy of this one.

11:37.780 --> 11:43.330
And we are going to go ahead and click on here and we'll say this is secure, underscore URL.

11:43.330 --> 11:44.800
So again, we get this from the cloud.

11:45.040 --> 11:45.820
No big deal.

11:45.850 --> 11:47.890
We'll be storing that as a string format.

11:47.890 --> 11:48.730
Required is true.

11:48.730 --> 11:50.290
Definitely 100%.

11:50.980 --> 11:52.780
So this is all the basics of photo.

11:53.920 --> 11:55.810
And let me go ahead and minimize.

11:55.810 --> 11:57.400
Actually, I need to copy this.

11:59.160 --> 12:00.390
Or I can copy the roll.

12:00.390 --> 12:01.550
Actually, that's much better.

12:01.560 --> 12:03.120
This is what we need.

12:03.570 --> 12:04.230
Okay, let's get.

12:04.230 --> 12:06.390
Grab that and I'll shrink this one.

12:07.740 --> 12:07.990
Okay.

12:08.040 --> 12:14.400
So after that, what we were having is let's check that we we're having this a reset password.

12:14.430 --> 12:19.290
Token I'm going to call this as forgot password token because I think that makes a bit more sense to

12:19.290 --> 12:19.730
me.

12:19.740 --> 12:21.930
So let's go ahead and call this one as.

12:22.260 --> 12:24.960
Forgot password.

12:25.500 --> 12:26.370
Feel free to name it.

12:26.370 --> 12:30.820
Anything that says really the least bothering case I guess for any one of us.

12:30.840 --> 12:36.780
Now this password token is definitely a is a simple type of string, so we don't need this much of thing.

12:36.780 --> 12:39.420
Again, I can go ahead and call this one as date.

12:39.810 --> 12:43.170
Remove this 100%, totally a valid syntax.

12:43.170 --> 12:47.370
But in case when you have just the type that you have to mention, this is not a type date.

12:47.400 --> 12:48.660
This is type A string.

12:48.780 --> 12:52.290
Getting ahead of myself, I was thinking about forgot password expiry.

12:52.290 --> 12:53.310
So again, sorry.

12:53.550 --> 12:57.750
So again, this is totally a fine thing, but in case when you have just the type to be mentioned,

12:57.750 --> 13:03.030
I think it is a better approach to go ahead and come up here and simply say that this is going to be

13:03.030 --> 13:04.240
a string.

13:04.260 --> 13:04.890
There we go.

13:04.890 --> 13:05.520
That's it.

13:05.910 --> 13:11.040
Now, once we have this one, obviously, let's mention the forgot password expiry as well.

13:11.070 --> 13:16.680
So let's go ahead and say that expiry and this is of a format of date.

13:16.680 --> 13:17.340
That's it.

13:17.730 --> 13:18.360
Okay.

13:18.570 --> 13:20.100
These are the basic fields that we got.

13:20.100 --> 13:23.610
Let's see what else we are missing up, created out, of course.

13:23.620 --> 13:25.320
Let's go ahead and add that.

13:25.650 --> 13:31.380
So at the very end of it, I'm going to go ahead and say, hey, we have a field at, created, at and

13:31.380 --> 13:34.560
in this field, let's go ahead and add a date.

13:34.920 --> 13:38.160
So this is going to be type of date.

13:38.160 --> 13:39.600
That's fine.

13:39.600 --> 13:42.270
And we don't want to actually work on this created date.

13:42.270 --> 13:45.920
Every single time we work on the back end, we just want to auto populate it.

13:45.930 --> 13:48.870
So for this, we are going to set a default value to this.

13:48.870 --> 13:51.330
So default is going to be day dot now.

13:51.720 --> 13:54.450
Now again, please don't mention data now like this.

13:54.450 --> 13:56.310
We don't want to run it as this time.

13:56.310 --> 13:59.280
We want to run it whenever this field is getting executed.

13:59.280 --> 14:03.810
So let's save this and quickly have a thing that how we are actually managing this.

14:03.960 --> 14:05.730
So let me shrink all of them.

14:05.730 --> 14:08.430
So let's make it will make much more sense to you.

14:09.360 --> 14:10.260
And there you go.

14:10.830 --> 14:12.530
So this is our model.

14:12.540 --> 14:12.840
Totally.

14:12.840 --> 14:14.430
It's really simple, really basic.

14:14.430 --> 14:20.310
We got the name, email, password, role photo and forgot password, forgot password, expiry created

14:20.310 --> 14:20.640
add.

14:20.640 --> 14:24.630
So this is the very basic model, really teeny tiny that we have worked on.

14:25.290 --> 14:30.000
And I told you when we were discussing about the brainstorming, it looked like we were doing so much,

14:30.000 --> 14:31.000
but it's not too much.

14:31.020 --> 14:33.750
Once you are totally focused on what you really need to do.

14:33.750 --> 14:34.740
So that's what we got.

14:34.770 --> 14:39.090
Now, once the course actually finishes up, you can just go ahead and add more entries.

14:39.090 --> 14:44.610
Maybe you want to add a person's address or something directly up here so you can go ahead and do that.

14:44.610 --> 14:47.340
But I think we are going to stay with the this much of the basics.

14:47.610 --> 14:49.350
So this is all what we have done.

14:49.350 --> 14:50.820
Again, this part is all done.

14:50.820 --> 14:52.080
Now it works.

14:52.080 --> 14:56.250
We need to work on this, all these methods, and I'll show you some of the interesting stuff.

14:56.250 --> 14:59.640
This is the part we are going to learn something new and something interesting up here.

14:59.730 --> 15:02.100
Let's go ahead and catch up in the next video.
