1
00:00:00,230 --> 00:00:00,590
All right.

2
00:00:00,590 --> 00:00:07,610
And up next, let's set up the validation layer so everything is cool if we're getting the values we're

3
00:00:07,610 --> 00:00:08,090
looking for.

4
00:00:08,090 --> 00:00:15,830
But of course, at the moment, I can skip the name, for example, send over here and I'll still create

5
00:00:15,830 --> 00:00:16,730
the user.

6
00:00:17,000 --> 00:00:21,560
And in order to fix it, we want to navigate to our middleware.

7
00:00:21,560 --> 00:00:30,350
And I strongly suggest to at least try setting it up yourself and hopefully by the end you'll see that

8
00:00:30,350 --> 00:00:33,290
it's actually not as difficult as it seems.

9
00:00:33,290 --> 00:00:39,380
Yes, there is technically a lot of code, but you can clearly see that there's a bunch of repetition

10
00:00:39,380 --> 00:00:40,190
as well.

11
00:00:40,520 --> 00:00:44,120
And effectively I want to come up with a name first.

12
00:00:44,180 --> 00:00:51,290
In my case, I'm going to go with validate, register input and yes, we want to export since again,

13
00:00:51,290 --> 00:00:52,790
we'll set it up as middleware.

14
00:00:53,180 --> 00:00:55,700
So validate register.

15
00:00:56,740 --> 00:01:00,880
Input and that one is equal to our function.

16
00:01:01,680 --> 00:01:03,750
Then we want to pass in our array.

17
00:01:03,750 --> 00:01:07,170
And then one by one, let's go down the list.

18
00:01:07,410 --> 00:01:13,530
Like I said, always, always, always use your model as the guideline.

19
00:01:13,650 --> 00:01:18,180
So for some reason I'm showing you the job model, but actually I want to look at this one.

20
00:01:18,180 --> 00:01:23,580
So we have name, email, password, and we also have last name, location and role.

21
00:01:23,610 --> 00:01:25,470
Now don't worry about the role.

22
00:01:25,500 --> 00:01:31,620
This one will handle a little bit differently, but these ones, at least the basic setup is going to

23
00:01:31,620 --> 00:01:34,950
be almost the same like we have for the job.

24
00:01:34,980 --> 00:01:37,560
We just need to provide the proper names.

25
00:01:37,560 --> 00:01:40,410
So in here, what is the first one?

26
00:01:40,410 --> 00:01:41,430
Well, name?

27
00:01:41,430 --> 00:01:42,090
Correct.

28
00:01:42,120 --> 00:01:44,190
What are the conditions I want to set up?

29
00:01:44,190 --> 00:01:46,560
Well, first of all, it shouldn't be empty.

30
00:01:46,890 --> 00:01:48,240
That's kind of a straightforward.

31
00:01:48,390 --> 00:01:48,870
Right.

32
00:01:48,900 --> 00:01:53,760
So then let's add also with message and we'll say name is required.

33
00:01:54,060 --> 00:01:54,780
Okay, good.

34
00:01:54,780 --> 00:01:55,530
Let's save that.

35
00:01:55,530 --> 00:01:58,290
And then like I said, let's go down the list.

36
00:01:58,890 --> 00:02:04,150
The same is going to be for password, the same for location and also the email.

37
00:02:04,150 --> 00:02:09,610
And yes, once we have that one in place, we'll add some additional features.

38
00:02:09,610 --> 00:02:14,500
So one, two, three and then in here, essentially just change the values.

39
00:02:14,500 --> 00:02:15,700
That's all you have to do.

40
00:02:15,730 --> 00:02:16,390
Email.

41
00:02:16,390 --> 00:02:17,740
Yep, same deal.

42
00:02:18,040 --> 00:02:24,040
Then you want to do the same thing for the password and also for.

43
00:02:25,250 --> 00:02:25,730
Location.

44
00:02:25,730 --> 00:02:26,030
Okay.

45
00:02:26,030 --> 00:02:28,190
That's the straight up setup.

46
00:02:28,250 --> 00:02:32,330
Then when it comes to email, what else we want to check?

47
00:02:32,900 --> 00:02:34,440
Well, two things.

48
00:02:34,460 --> 00:02:37,760
We first want to check whether the.

49
00:02:38,740 --> 00:02:42,670
Email is actually valid email and what's super, super nifty.

50
00:02:42,700 --> 00:02:46,810
The middleware we're using actually has a method for that.

51
00:02:46,810 --> 00:02:51,280
So let's look for the email validation and we just want to chain it.

52
00:02:51,580 --> 00:02:53,230
I also want to go with is email.

53
00:02:53,230 --> 00:02:58,420
So whether it is a valid email and then what message I want to use.

54
00:02:58,450 --> 00:03:02,890
I'm going to say invalid email and format.

55
00:03:03,310 --> 00:03:13,240
And lastly, like I said, we also want to check whether there is already a user with the same email.

56
00:03:13,240 --> 00:03:20,540
So I want to make sure that when user registers only the unique email is used.

57
00:03:20,560 --> 00:03:21,880
So how we can do that?

58
00:03:21,880 --> 00:03:26,610
Well, you can probably already guess that we'll be setting up our own custom one.

59
00:03:26,620 --> 00:03:29,770
It is going to be async since we're.

60
00:03:30,350 --> 00:03:33,260
Going to check in our database.

61
00:03:33,260 --> 00:03:41,300
And remember, when it comes to a value in this callback function, essentially we can access whether

62
00:03:41,300 --> 00:03:46,910
that is the parameter or here or in this case, it's going to be the actual email value.

63
00:03:46,910 --> 00:03:50,390
So the general concept doesn't change.

64
00:03:50,420 --> 00:03:53,570
It's just in this case, of course we'll be accessing the email.

65
00:03:53,570 --> 00:03:59,030
And as far as the logic, well we'll need to grab the user.

66
00:03:59,270 --> 00:04:01,010
So let's navigate up.

67
00:04:01,160 --> 00:04:03,830
We're looking for the user and this is coming from.

68
00:04:04,800 --> 00:04:06,780
User model.

69
00:04:06,810 --> 00:04:12,450
Okay, let's scroll down then Let's look for our custom function.

70
00:04:12,660 --> 00:04:16,589
And as far as the logic, I'm going to go here with const user.

71
00:04:17,350 --> 00:04:20,769
And we're going to go with Await then user.

72
00:04:20,779 --> 00:04:23,830
And in this case, I'm going to go with find one.

73
00:04:23,830 --> 00:04:26,830
So not find by ID not find.

74
00:04:26,830 --> 00:04:32,290
I'm going to go with find one which is going to look, for instance, based on some kind of condition

75
00:04:32,390 --> 00:04:34,030
in my case that is email.

76
00:04:34,060 --> 00:04:37,030
So look for user.

77
00:04:37,750 --> 00:04:40,360
Based on this email, whatever is the value.

78
00:04:40,360 --> 00:04:45,490
And of course, if the user is already there, then I want to throw the error.

79
00:04:45,730 --> 00:04:52,090
Again, this is the case where you can throw the regular JavaScript error because again, this will

80
00:04:52,090 --> 00:04:54,310
be handled over here.

81
00:04:54,990 --> 00:04:58,410
Or you can go with custom one, which is going to be my approach.

82
00:04:58,410 --> 00:05:00,210
So let's set up our condition.

83
00:05:00,210 --> 00:05:10,650
So if the user exists, we want to go with throw new bad request error and we'll just write email already.

84
00:05:11,490 --> 00:05:13,020
Exists like so.

85
00:05:13,020 --> 00:05:16,110
And if the user doesn't exist, we don't need to do anything.

86
00:05:16,140 --> 00:05:20,400
Again, this is a sync function, so we don't need to worry about returning.

87
00:05:20,430 --> 00:05:22,320
True or false?

88
00:05:22,320 --> 00:05:27,030
And then when it comes to password, we can also utilize some.

89
00:05:27,420 --> 00:05:30,540
Cool features from our library.

90
00:05:30,540 --> 00:05:35,310
So again, right after with message, we're going to chain one more method.

91
00:05:35,310 --> 00:05:38,400
And in this case I'm going to go for is length.

92
00:05:40,260 --> 00:05:45,870
As far as the options, I'm going to go with men and I'll set it equal to eight.

93
00:05:45,900 --> 00:05:50,730
Now, when it comes to the actual message, again, let's chain it.

94
00:05:51,210 --> 00:05:54,600
Let's say with message and let's just write.

95
00:05:54,600 --> 00:05:58,470
Password must be at least eight characters long.

96
00:05:58,710 --> 00:06:05,390
And the last thing we want to do is to navigate to our routes.

97
00:06:05,760 --> 00:06:07,020
We're looking for.

98
00:06:08,040 --> 00:06:15,330
Auth router and let's look for our validate and register input.

99
00:06:16,060 --> 00:06:19,570
And now let's place it right before register.

100
00:06:19,930 --> 00:06:22,630
So let's set up the come over here.

101
00:06:23,190 --> 00:06:27,570
Now, of course, if you want, you can clean up the database again, because I can tell you right away

102
00:06:27,570 --> 00:06:29,220
that this is going to fail.

103
00:06:29,250 --> 00:06:29,710
Why?

104
00:06:29,730 --> 00:06:32,880
Well, because we have condition right now for this email.

105
00:06:32,880 --> 00:06:37,830
So once I save notice, name is required, but email already exists.

106
00:06:37,830 --> 00:06:44,040
So of course I can go back over here and add John, but I'll still have this email already exists,

107
00:06:44,040 --> 00:06:46,200
so I'll have to change it to something.

108
00:06:46,200 --> 00:06:52,350
And this is the case that yes, while we're creating our first users, basically before we set up the

109
00:06:52,350 --> 00:06:57,300
login, I will also be removing those users from the database because it's just easier.

110
00:06:57,300 --> 00:06:58,860
But I do want to showcase that.

111
00:06:58,920 --> 00:07:02,010
Of course, if I change it to Peter, then everything is correct.

112
00:07:02,040 --> 00:07:04,520
Then we're able to create the user.

113
00:07:04,530 --> 00:07:10,140
Now, as far as the other errors, I'm not sure whether I'll test all of them, but for example, for

114
00:07:10,140 --> 00:07:12,330
the password, check it out.

115
00:07:12,330 --> 00:07:18,450
I can go here and of course it goes with 400 because the user already exists.

116
00:07:18,450 --> 00:07:20,370
So now let me go to my database.

117
00:07:20,610 --> 00:07:25,300
Let me start from scratch because I don't want to keep changing those emails again.

118
00:07:25,300 --> 00:07:27,070
What's really, really awesome.

119
00:07:27,100 --> 00:07:31,510
We can just navigate over here and we can drop everything.

120
00:07:31,510 --> 00:07:34,450
So in this case, of course, we're looking for users.

121
00:07:34,870 --> 00:07:41,110
Then let me go to my Thunder client and like I said, what I'm trying to showcase is that if the password

122
00:07:41,110 --> 00:07:45,070
is going to be less than eight characters, there's going to be an error.

123
00:07:45,070 --> 00:07:49,060
So let me remove the numbers from the password.

124
00:07:50,040 --> 00:07:52,380
Me send and check it out.

125
00:07:52,380 --> 00:07:56,850
Password must be at least eight characters long.

126
00:07:56,940 --> 00:08:04,230
And I think at the very end of this video, I just want to showcase what happens if the syntax for email

127
00:08:04,260 --> 00:08:06,840
does not match what the library is looking for.

128
00:08:06,840 --> 00:08:11,550
So, for example, if I'm going to go with just Gmail.

129
00:08:12,230 --> 00:08:12,920
Check it out.

130
00:08:12,920 --> 00:08:15,250
Invalid email format.

131
00:08:15,260 --> 00:08:17,750
So hopefully this is clear.

132
00:08:17,900 --> 00:08:18,890
One last thing.

133
00:08:18,890 --> 00:08:24,320
It looks like I forgot to add validation for last name field during the video.

134
00:08:24,470 --> 00:08:33,169
So before moving on to the next task, go back to validate, register input and add one more condition

135
00:08:33,530 --> 00:08:34,700
As far as the logic.

136
00:08:34,730 --> 00:08:37,600
It's exactly the same as for location.

137
00:08:37,610 --> 00:08:42,559
Just change the name and make sure field value is camel case.

138
00:08:42,559 --> 00:08:44,030
So last name.

139
00:08:44,210 --> 00:08:49,640
If you need a reference, please utilize the Readme since it contains the complete code.

