1
00:00:07,540 --> 00:00:08,680
In this video.

2
00:00:08,680 --> 00:00:12,840
Let's see how we can update a user profile.

3
00:00:12,850 --> 00:00:17,110
So inside the user controller, we have a dummy root.

4
00:00:17,110 --> 00:00:22,810
And here is the update line number 89 or nine code.

5
00:00:22,900 --> 00:00:27,670
So let's expand it and now let's test the endpoints if it exists.

6
00:00:27,670 --> 00:00:34,870
And this is the endpoint when I hit ST, indeed the root has been called, so let's see how we are going

7
00:00:34,870 --> 00:00:37,150
to implement the update.

8
00:00:37,150 --> 00:00:39,250
So here we go.

9
00:00:39,580 --> 00:00:48,860
Remember, if we update a user field with a password, it means that we are changing the details about

10
00:00:48,860 --> 00:00:51,880
the user, including the password.

11
00:00:51,970 --> 00:00:56,740
So with this one, unless we rehash the new password.

12
00:00:56,830 --> 00:00:59,740
So first we are going to make some checking.

13
00:00:59,740 --> 00:01:08,800
One, if a user provide a password field, then you go ahead and then re hash the new password.

14
00:01:08,890 --> 00:01:14,050
Or if a user is updating, add our field apart from the password.

15
00:01:14,050 --> 00:01:16,540
We need to also handle that.

16
00:01:16,540 --> 00:01:18,580
So let's have the steps.

17
00:01:18,610 --> 00:01:26,730
Like I said, since we have the user being log in and we are going to protect this endpoint that we

18
00:01:26,740 --> 00:01:30,310
need to have the user and that red dot user.

19
00:01:30,310 --> 00:01:34,300
So with that one, we don't need ID as we see here.

20
00:01:34,300 --> 00:01:39,370
So let's get back to the root and the users and now let's make changes.

21
00:01:39,370 --> 00:01:44,290
So here we go, let's locate the update, which is this one.

22
00:01:44,290 --> 00:01:51,160
So now let's remove the ID because we want to have the ID inside the red dot user.

23
00:01:51,160 --> 00:01:55,120
So now let's go to the controller and let's look at the step.

24
00:01:55,540 --> 00:02:03,190
Step number one is that we are going to check the email the user is trying to update to.

25
00:02:03,220 --> 00:02:12,580
Let's say that originally the email is a at gmail.com and then for updating the user is trying to change

26
00:02:12,580 --> 00:02:16,600
from a dot com to let's say B at gmail.com.

27
00:02:17,050 --> 00:02:23,200
It can happen that B at gmail.com has been used by different person in that way.

28
00:02:23,200 --> 00:02:24,610
We need to avoid that.

29
00:02:24,610 --> 00:02:30,310
So here are going to be check if email exists.

30
00:02:30,310 --> 00:02:32,800
So here we go const.

31
00:02:34,470 --> 00:02:35,700
User found.

32
00:02:37,110 --> 00:02:45,270
Is equal to a wait and for the user model they are going to find by ID and the ID can be found on that

33
00:02:45,270 --> 00:02:47,070
rec dot user.

34
00:02:47,250 --> 00:02:49,200
So here we go.

35
00:02:49,440 --> 00:02:51,870
So let's go ahead and throw some error.

36
00:02:51,900 --> 00:02:54,210
In case this user name exists.

37
00:02:54,210 --> 00:03:03,350
So here, if the user found, then we are going to return and then you pass the error to next.

38
00:03:03,360 --> 00:03:08,400
Because of that, let's add next to the arguments as that.

39
00:03:08,400 --> 00:03:15,030
So here we call next and now we pass in our instance of our app error handler.

40
00:03:15,210 --> 00:03:29,220
Then we pass in the error name as user name Xs or email is ticking or exists and then let's provide

41
00:03:29,220 --> 00:03:31,720
the status code as 400.

42
00:03:31,740 --> 00:03:33,600
So now the next step.

43
00:03:34,050 --> 00:03:43,170
The next step is that check if user is updating the password.

44
00:03:44,190 --> 00:03:46,200
So here we go.

45
00:03:46,620 --> 00:03:50,610
If rec dot body dot password.

46
00:03:50,610 --> 00:03:57,570
Meaning if the user is providing password, now you go ahead and hash the user password.

47
00:03:57,570 --> 00:04:03,030
So as we did for registration, we need a source equal to a weight.

48
00:04:03,030 --> 00:04:08,400
And then the big package dot gen sort, they pass in ten.

49
00:04:08,670 --> 00:04:17,519
After that we want to have the hashed password as hashed password is equal to a weight.

50
00:04:17,519 --> 00:04:22,079
And now we bring in the big quick package dot hash.

51
00:04:22,440 --> 00:04:29,280
Then you pass in the new password that is rec dot, body dot password.

52
00:04:29,460 --> 00:04:34,020
And lastly, we go ahead and then update the user.

53
00:04:34,050 --> 00:04:38,730
So here we go, updates the user.

54
00:04:39,120 --> 00:04:43,770
So here const user is equal to a weight.

55
00:04:43,800 --> 00:04:53,400
Now we are going to find the user by ID and then update the pass in the rec dot user.

56
00:04:53,760 --> 00:04:57,960
So here we pass in the field, the user is trying to update.

57
00:04:57,990 --> 00:05:04,560
So for this one is a password field is equal to the hashed password.

58
00:05:04,590 --> 00:05:11,760
The next argument that this function takes is going to be the new attribute because you want to see

59
00:05:11,760 --> 00:05:14,460
the updated data on the fly.

60
00:05:14,490 --> 00:05:18,060
So here we provide new to true.

61
00:05:18,360 --> 00:05:25,450
And next is we want to run the validation in case a user refuses to provide the password field and going

62
00:05:25,450 --> 00:05:26,460
to be true.

63
00:05:26,970 --> 00:05:31,350
So the last step is that we need to send a response to the user.

64
00:05:31,350 --> 00:05:35,610
So let's go last this one and now here is the response.

65
00:05:35,610 --> 00:05:46,080
So here send the response and here we go, raise dirt status, then provide 200, meaning everything

66
00:05:46,080 --> 00:05:46,940
is okay.

67
00:05:46,950 --> 00:05:49,500
And here is our JSON data.

68
00:05:49,620 --> 00:05:59,160
You provide the status as success and then the data as the user, and that is it.

69
00:05:59,190 --> 00:06:02,480
So this is for the password handling, right?

70
00:06:02,490 --> 00:06:09,230
So next is if a user refused to provide a password, but the person is trying to update the other properties.

71
00:06:09,240 --> 00:06:14,460
So let's see, const user is equal to our weight.

72
00:06:14,460 --> 00:06:22,500
And then for the user model, I'm going to find by ID and update and the ID is under rec the user.

73
00:06:22,530 --> 00:06:30,000
And here we are going to use the entire request body for the updating you pass in the rec dot body.

74
00:06:30,000 --> 00:06:41,190
And lastly, you pass in the configuration as new to true and run validation also to true and that is

75
00:06:41,190 --> 00:06:41,550
it.

76
00:06:41,580 --> 00:06:43,980
Lastly, let's send the response.

77
00:06:43,980 --> 00:06:52,950
We can copy this one and down here we can remove this one and paste this one here the say response.

78
00:06:52,950 --> 00:07:00,540
So now we are done with the concept, a thing where we need to pass the source and the second element

79
00:07:00,540 --> 00:07:01,620
to the hash.

80
00:07:01,620 --> 00:07:06,300
So guys, so let's go ahead and then protect this controller.

81
00:07:06,330 --> 00:07:10,860
So inside the user's root, here we go.

82
00:07:10,920 --> 00:07:13,920
And now let's locate the update.

83
00:07:13,950 --> 00:07:19,530
So you're going to pass in a log in mature as is logged in.

84
00:07:23,400 --> 00:07:24,540
And come on.

85
00:07:24,690 --> 00:07:27,630
So now it's time to test it, remember?

86
00:07:27,660 --> 00:07:29,010
The path has changed.

87
00:07:29,010 --> 00:07:34,380
So is no more an ID here, but instead something like this.

88
00:07:34,590 --> 00:07:38,760
So with this one, let's try to make a request without passing in any payload.

89
00:07:38,760 --> 00:07:42,030
And let's see the error where cannot get.

90
00:07:42,330 --> 00:07:45,590
It means that we are not doing something right.

91
00:07:45,600 --> 00:07:47,810
So inside the user's route.

92
00:07:47,820 --> 00:07:49,290
Where is this one?

93
00:07:49,290 --> 00:07:51,210
So let's remove the ID.

94
00:07:51,540 --> 00:07:52,350
Yes.

95
00:07:52,920 --> 00:07:53,430
Good.

96
00:07:53,430 --> 00:08:00,750
So let's again try and get rid of split because in the past, in the token.

97
00:08:00,960 --> 00:08:04,950
So let's go ahead and then look in from here.

98
00:08:04,980 --> 00:08:06,600
And here we go.

99
00:08:06,630 --> 00:08:14,400
We have the user code John and our grab John token and then here for the update, let's go ahead and

100
00:08:14,400 --> 00:08:17,070
provide that under headers.

101
00:08:17,070 --> 00:08:26,010
And here you bring in authorization and bearer and I'll paste the token and let's get sent where we

102
00:08:26,010 --> 00:08:31,740
can see that email is taking meaning and providing an empty email.

103
00:08:31,830 --> 00:08:33,210
So here you go.

104
00:08:33,480 --> 00:08:37,020
Let's say I want to update the foo name.

105
00:08:37,020 --> 00:08:41,700
So here as foo name and I want to change this one to change.

106
00:08:45,180 --> 00:08:53,400
And now let's send and you can see we have message email is taken meaning that we are doing something

107
00:08:53,400 --> 00:08:54,150
right.

108
00:08:54,300 --> 00:09:00,660
So let's get to the controller and let's see where the error is coming from here.

109
00:09:00,660 --> 00:09:03,900
So I think we have something wrong about this one.

110
00:09:04,230 --> 00:09:12,510
Well, we are not going to find the user by the ID, but instead we want to check if a user is trying

111
00:09:12,510 --> 00:09:17,550
to provide the email as a way of updating.

112
00:09:17,550 --> 00:09:27,090
So here we are going to find the user by email and as rec dot body dot email.

113
00:09:27,390 --> 00:09:27,930
Great.

114
00:09:27,930 --> 00:09:30,000
So let's save it now.

115
00:09:30,000 --> 00:09:31,080
Let's check it out.

116
00:09:31,080 --> 00:09:37,680
When I hit send, let's comment this one and I'll show you as we move on.

117
00:09:37,680 --> 00:09:41,220
So now in this one, I can go ahead and make a request.

118
00:09:41,220 --> 00:09:46,110
And now you can see that I have updated the full name to change.

119
00:09:46,140 --> 00:09:56,580
BLOCK So again, if I try to update the email field to somebody's email, which has been taken already,

120
00:09:56,580 --> 00:10:02,490
so I think I have this email or at home at gmail.com or better.

121
00:10:02,490 --> 00:10:08,850
So let's check inside our collection of a database and let's see, let me connect my DB.

122
00:10:08,880 --> 00:10:11,970
I want to look for email which has been registered.

123
00:10:12,540 --> 00:10:15,840
So here users and documents.

124
00:10:16,200 --> 00:10:18,960
Let me check this one I have John.

125
00:10:18,960 --> 00:10:20,630
No, I want outbound.

126
00:10:20,700 --> 00:10:25,770
Yes, you can see that this email has been taken by different person.

127
00:10:25,770 --> 00:10:33,330
So if I try to update to this email, let's see now back to the update.

128
00:10:33,540 --> 00:10:36,450
So I want to change from John to add on.

129
00:10:36,450 --> 00:10:41,640
But for this item here, it has been taken by a different person, but I so I can go ahead and then

130
00:10:41,640 --> 00:10:42,330
change it.

131
00:10:42,420 --> 00:10:44,640
That is what I want to avoid it.

132
00:10:44,640 --> 00:10:51,630
So inside the user's controller for this, we can make some conditions here before returning an error.

133
00:10:51,630 --> 00:10:53,860
So here let's check that.

134
00:10:53,910 --> 00:11:02,820
We want to check if a user is trying to provide the email field as rec, dot, body, dot email, then

135
00:11:02,820 --> 00:11:05,400
we are going to call this functionality.

136
00:11:05,400 --> 00:11:11,040
So inside here I'll paste that and now let's go ahead and add it again.

137
00:11:11,040 --> 00:11:20,070
So let's hit send where I think the error is coming from here we are finding the user by ID, but our

138
00:11:20,070 --> 00:11:21,690
criteria says email.

139
00:11:21,720 --> 00:11:24,420
So let's change this one to find one.

140
00:11:24,930 --> 00:11:29,760
So for find one, we can find a data based on any property.

141
00:11:29,760 --> 00:11:32,430
So with this one, I think we are good to go.

142
00:11:32,430 --> 00:11:37,230
So let's hit send and you can see that this email is taken.

143
00:11:41,360 --> 00:11:46,880
So we can modify the message by saying that e-mail is taking all.

144
00:11:48,040 --> 00:11:54,730
You already have this email.

145
00:11:55,120 --> 00:11:55,480
All right.

146
00:11:55,480 --> 00:12:00,040
So let's try and send again and let's look at the error and there you go.

147
00:12:00,100 --> 00:12:05,350
We have it as email mail is taking or you already have this email.

148
00:12:05,470 --> 00:12:08,800
So let's try to change the password now.

149
00:12:08,830 --> 00:12:12,810
The password still remains the same as one, two, three, four, five.

150
00:12:12,820 --> 00:12:14,700
And now the email is this.

151
00:12:14,710 --> 00:12:21,010
So let's go ahead and then log in and now let me remove this one and use the ATM.

152
00:12:21,040 --> 00:12:29,200
Now let's say you can see that everything remains the same, but if I change the email and let's see

153
00:12:29,200 --> 00:12:31,570
this email doesn't exist.

154
00:12:31,600 --> 00:12:37,210
If email is correct and the password is incorrect, we get the same result.

155
00:12:37,420 --> 00:12:41,520
So let's go ahead and then update the password also.

156
00:12:41,530 --> 00:12:50,620
So inside the update this time around, I won't remove the email and then update the password as that.

157
00:12:54,770 --> 00:13:00,250
And I would change this 1 to 1 two and let's hit St now in Sussex.

158
00:13:00,260 --> 00:13:07,760
Let me log in with a format password and now I think let me check my server here.

159
00:13:08,270 --> 00:13:09,680
Oh, I got some error here.

160
00:13:09,680 --> 00:13:13,370
Meaning that cannot set headers after they are sent to the client.

161
00:13:13,370 --> 00:13:17,570
Meaning that inside here we need to return something.

162
00:13:17,570 --> 00:13:20,120
That is an update which is.

163
00:13:20,120 --> 00:13:22,670
Let me see where we are sending.

164
00:13:23,330 --> 00:13:25,460
Let me check for the password yet.

165
00:13:25,460 --> 00:13:28,670
This one, we need to ten return here.

166
00:13:28,700 --> 00:13:30,710
Yes, and that is it.

167
00:13:30,710 --> 00:13:31,850
It will fix that.

168
00:13:32,060 --> 00:13:33,920
So let me try again.

169
00:13:34,490 --> 00:13:34,910
Okay.

170
00:13:35,030 --> 00:13:36,440
Now is correct.

171
00:13:36,440 --> 00:13:41,390
Let me log in with a format password and a C invalid.

172
00:13:41,390 --> 00:13:48,050
If I change this 1 to 1, two and let's see now it works.

