1
00:00:00,020 --> 00:00:00,380
All right.

2
00:00:00,410 --> 00:00:03,980
Up next, let's work on update user functionality.

3
00:00:03,980 --> 00:00:09,070
And this one is going to be extremely similar to update job.

4
00:00:09,080 --> 00:00:14,180
However, we're not looking for the ID in req.params.

5
00:00:14,210 --> 00:00:21,980
It's actually going to be available in rec, dot user user ID and also we won't send back the.

6
00:00:22,790 --> 00:00:27,840
Updated user essentially in order to check the changes.

7
00:00:27,860 --> 00:00:35,330
We'll call get current user, which gets the current user because that's how our front end is going

8
00:00:35,330 --> 00:00:35,720
to work.

9
00:00:35,720 --> 00:00:37,100
So let's try it out.

10
00:00:37,130 --> 00:00:39,410
I'm going to go to user controller.

11
00:00:40,210 --> 00:00:41,890
We're looking for this one over here.

12
00:00:42,040 --> 00:00:45,130
And essentially let's go with const updated.

13
00:00:45,630 --> 00:00:46,470
User.

14
00:00:47,420 --> 00:00:48,770
That is going to be equal to a.

15
00:00:49,220 --> 00:01:00,240
Then user find by ID and update and let's pass in req dot user, user id, comma and then rec dot body.

16
00:01:00,260 --> 00:01:03,050
So essentially this is where we'll provide all of the values.

17
00:01:03,050 --> 00:01:07,600
And again, we're not going to be sending back this updated user.

18
00:01:07,610 --> 00:01:15,320
So in order to check the changes, we will use get current user and also in the Readme, we added Json.

19
00:01:15,320 --> 00:01:21,960
So that way you can speed this up a little bit as far as providing the values in the body.

20
00:01:21,980 --> 00:01:28,520
So now let's go to update user request and like I said, I'm just going to copy and paste.

21
00:01:28,730 --> 00:01:32,880
I believe I'm changing here the location from my city to Florida.

22
00:01:32,930 --> 00:01:36,320
So let me send if everything is correct.

23
00:01:37,080 --> 00:01:41,390
We can move on to the next step where we want to get the current user.

24
00:01:41,400 --> 00:01:48,810
So this is basically, John, before we updated the user and once I send it, notice now the location

25
00:01:48,810 --> 00:01:56,340
is Florida and everything is awesome, but there is a major piece missing and that is validation because

26
00:01:56,340 --> 00:02:02,280
keep in mind, just like when we registered the user, also when we update, we want to make sure that

27
00:02:02,280 --> 00:02:06,450
the values that are coming in are exactly what we're expecting.

28
00:02:06,450 --> 00:02:12,450
So now let's quickly navigate to validation middleware and let's create a new one.

29
00:02:12,480 --> 00:02:18,480
Now the good news is that we already have validation for register and most of the stuff is going to

30
00:02:18,480 --> 00:02:19,920
be exactly the same.

31
00:02:19,920 --> 00:02:26,250
So first we just want to come up with the value and let's go here with Validate and I'm going to call

32
00:02:26,250 --> 00:02:29,370
this update user input.

33
00:02:30,600 --> 00:02:33,090
Remember, we want to invoke our function.

34
00:02:33,870 --> 00:02:36,510
And as far as the values, let's just scroll up.

35
00:02:37,180 --> 00:02:39,010
We're looking for a registered one.

36
00:02:39,460 --> 00:02:41,200
Let's grab all of these.

37
00:02:42,000 --> 00:02:45,030
Lines of code, copy and paste.

38
00:02:45,120 --> 00:02:50,040
So the only thing we're going to change here is the logic for email.

39
00:02:50,310 --> 00:02:52,000
We'll use a different approach.

40
00:02:52,020 --> 00:02:57,510
And also, when it comes to a password, we're actually not going to check it.

41
00:02:57,540 --> 00:03:02,520
So for now, we have location, last name, also name and email.

42
00:03:02,520 --> 00:03:05,460
So all of this stays the same.

43
00:03:05,490 --> 00:03:08,310
The only thing we'll change is the custom one.

44
00:03:08,650 --> 00:03:12,150
And as far as the logic, this line will stay exactly the same.

45
00:03:12,150 --> 00:03:15,750
But here in the condition, we'll add some logic.

46
00:03:15,840 --> 00:03:22,800
Basically, I'm going to go with and then I'll look for user underscore ID, Then I'll transfer this

47
00:03:22,800 --> 00:03:30,990
one to string and then I'll say if it's not equal to req dot user user ID.

48
00:03:31,930 --> 00:03:34,540
Now, why do we need to add this line of code?

49
00:03:34,570 --> 00:03:39,400
Well, because remember, this deals with that unique email.

50
00:03:39,430 --> 00:03:40,240
Correct.

51
00:03:40,810 --> 00:03:50,140
And of course, if I'm sending in John at gmail.com, if I'll just check whether a user exists based

52
00:03:50,140 --> 00:03:52,570
on this email, of course it's going to be a user.

53
00:03:52,600 --> 00:03:53,650
It's me.

54
00:03:54,190 --> 00:03:58,900
So with this line of code, we're just making sure that user exists.

55
00:03:59,610 --> 00:04:01,950
But the user is not us.

56
00:04:01,950 --> 00:04:10,710
So essentially, if I'm sending in some kind of email address, I'm checking for other users and whether

57
00:04:10,710 --> 00:04:14,190
they are not already using this email address.

58
00:04:14,220 --> 00:04:15,810
Hopefully that makes sense.

59
00:04:15,810 --> 00:04:17,610
So this stays the same.

60
00:04:18,410 --> 00:04:20,690
Again, we'll send back the error.

61
00:04:20,690 --> 00:04:25,760
We just want to quickly jump to a router, I believe.

62
00:04:26,290 --> 00:04:33,570
So let's go to user router and we want to add here this validate.

63
00:04:33,580 --> 00:04:40,480
So right before the update user, we're going to go with validate, update user input.

64
00:04:40,480 --> 00:04:42,070
And now let's try it out.

65
00:04:42,070 --> 00:04:45,250
So I'm going to navigate to update user.

66
00:04:46,030 --> 00:04:46,810
And you know what?

67
00:04:46,810 --> 00:04:48,550
Let's go with.

68
00:04:50,010 --> 00:04:51,030
No last name.

69
00:04:51,030 --> 00:04:52,570
So let me remove it here.

70
00:04:52,590 --> 00:04:54,000
Let me send a notice.

71
00:04:54,000 --> 00:04:55,680
Last name is required.

72
00:04:55,710 --> 00:04:59,850
Now, please keep in mind that again, on the front end, everything is going to be set up where we

73
00:04:59,850 --> 00:05:01,470
are sending these values.

74
00:05:01,500 --> 00:05:06,840
We just have validations in place so we get the correct ones.

75
00:05:06,840 --> 00:05:14,250
And for example, if I'm going to go here with Peter at gmail.com, I'm going to get back the bad request.

76
00:05:14,280 --> 00:05:14,820
Why?

77
00:05:14,850 --> 00:05:21,210
Well, because peter@gmail.com is already used by another user.

78
00:05:21,210 --> 00:05:25,020
That's why we added that additional logic over here.

79
00:05:25,020 --> 00:05:32,760
However, if I go with John, of course the user already exists, but that user is me.

80
00:05:32,760 --> 00:05:34,800
So of course that is allowed.

81
00:05:34,890 --> 00:05:36,810
And the last thing that I want to do.

82
00:05:37,700 --> 00:05:45,500
As far as the user controller, I just want to remove from the body the password since I don't have

83
00:05:45,500 --> 00:05:53,220
any validation and I don't want the user to update password with this particular controller.

84
00:05:53,250 --> 00:05:54,440
How we can do that?

85
00:05:54,470 --> 00:05:58,510
Well, first we need to come up with some kind of value.

86
00:05:58,520 --> 00:06:01,310
In my case, again, I'm going to go with Object over here.

87
00:06:01,340 --> 00:06:06,950
Then we will spread out all of the properties we have in Req.body.

88
00:06:06,950 --> 00:06:10,940
So we're going to go with dot, dot, dot then req dot body.

89
00:06:11,930 --> 00:06:13,790
And then let's go with object.

90
00:06:13,790 --> 00:06:16,640
And remember the keyword was delete.

91
00:06:17,180 --> 00:06:19,670
So pretty much just like we did over here.

92
00:06:20,260 --> 00:06:22,120
When we were setting up that instance.

93
00:06:22,120 --> 00:06:22,540
One.

94
00:06:22,540 --> 00:06:26,830
I just want to remove this property just in case it's being passed.

95
00:06:27,310 --> 00:06:31,210
So let's go with password just in case it exists.

96
00:06:31,210 --> 00:06:33,910
And then instead of req.body.

97
00:06:34,730 --> 00:06:37,250
We're just going to pass in the object over here.

98
00:06:37,430 --> 00:06:41,480
So that's the last thing that I want to actually check it out.

99
00:06:42,200 --> 00:06:44,960
Let me log what I have as far as the object.

100
00:06:44,960 --> 00:06:47,480
And let me add here this password.

101
00:06:48,110 --> 00:06:53,750
So we're going to go here with password and let's set it equal to secret.

102
00:06:54,140 --> 00:06:55,580
We also need to add a comma.

103
00:06:55,610 --> 00:06:57,590
Let me send it over here.

104
00:06:57,590 --> 00:07:02,190
And then if everything is correct, notice we don't have this password.

105
00:07:02,210 --> 00:07:09,410
So essentially, we just make sure that the password value doesn't get added to our.

106
00:07:10,160 --> 00:07:12,590
Update user functionality.

107
00:07:12,590 --> 00:07:16,130
And with this in place, we can move on to the next step.

