1
00:00:00,570 --> 00:00:06,570
In this video, we are going to create a controller for updating a user password.

2
00:00:06,570 --> 00:00:08,520
So let's get back to the controllers.

3
00:00:08,520 --> 00:00:12,390
And for users, let's locate the damage controller.

4
00:00:12,390 --> 00:00:14,370
So let's collapse this one.

5
00:00:14,370 --> 00:00:19,530
And this is what we have as an update password controller.

6
00:00:19,650 --> 00:00:21,690
So let's look at the step.

7
00:00:21,810 --> 00:00:30,000
Step number one is less the structure, the password the user is trying to update from red dot body

8
00:00:30,150 --> 00:00:33,210
and here I'll have access to the password.

9
00:00:33,660 --> 00:00:39,210
So the first step is that let's check if the user is updating the password.

10
00:00:39,210 --> 00:00:41,040
So the comment goes like this.

11
00:00:41,040 --> 00:00:50,220
And here, if indeed the user is updating the password, then we are going to rehash the password again.

12
00:00:50,220 --> 00:01:00,240
So going to be as const and then sorts is equal to await the package copy scripts and we have what is

13
00:01:00,240 --> 00:01:02,730
called gen sort pass in ten.

14
00:01:02,880 --> 00:01:07,890
The process is the same when we are hashing the user password upon registration.

15
00:01:07,890 --> 00:01:13,230
So here is going to be as password hashed is equal to a weight.

16
00:01:13,230 --> 00:01:22,140
Then the package called be crypt dot hash, then you pass in the plane password and then the source.

17
00:01:22,650 --> 00:01:26,520
So next step is that we need to update the user.

18
00:01:26,520 --> 00:01:28,050
So here we go.

19
00:01:28,080 --> 00:01:42,120
UPDATE and we are going to await the user dot find by ID and update and the ID can be found under red

20
00:01:42,150 --> 00:01:48,750
dot params, dot ID and then the fields I want to update for this one.

21
00:01:48,750 --> 00:01:55,700
I want to update the password only and I want to assign to the password hashed.

22
00:01:57,230 --> 00:02:01,790
Then the additional configuration as new to true meaning.

23
00:02:01,790 --> 00:02:04,760
I want to have the updated record.

24
00:02:04,790 --> 00:02:05,480
All right.

25
00:02:05,480 --> 00:02:12,500
So the last step is that let's send this message by saying that password has been changed successfully

26
00:02:12,980 --> 00:02:14,630
and here we go.

27
00:02:15,110 --> 00:02:20,510
So for the error here, we are going to send our custom error by calling next.

28
00:02:20,510 --> 00:02:22,370
And here we go.

29
00:02:24,910 --> 00:02:29,080
So I need to pass in next as a third argument to this.

30
00:02:29,770 --> 00:02:36,190
And down here called next and I'll pass in the app error and then the message.

31
00:02:36,190 --> 00:02:39,820
So here I will say please provide password field.

32
00:02:40,330 --> 00:02:40,840
All right.

33
00:02:40,840 --> 00:02:43,420
So let's go ahead and make the request.

34
00:02:43,420 --> 00:02:53,380
So inside the update password, which is this let's provide the write ID so I can use.

35
00:02:53,380 --> 00:02:58,690
Let me locate the log in user here and let's grab the ID.

36
00:02:59,460 --> 00:03:07,380
So I'm going to grab this one and the update endpoint, which is password update.

37
00:03:07,650 --> 00:03:10,920
Let me provide the ID and then the body.

38
00:03:10,920 --> 00:03:19,230
I want to change the password from one, two, three, four, 5 to 1, two, three and hit send where

39
00:03:19,230 --> 00:03:20,280
we go back.

40
00:03:20,280 --> 00:03:25,380
Please provide password field meaning that we are doing something wrong.

41
00:03:25,410 --> 00:03:33,590
So inside the update password controller, we need to bring this logic inside the if statement here.

42
00:03:33,600 --> 00:03:37,320
So let's go ahead and then cut the logic from here.

43
00:03:39,320 --> 00:03:43,610
And let's place it inside the if statement.

44
00:03:43,610 --> 00:03:49,160
So let me bring the comment above here as update user.

45
00:03:49,310 --> 00:03:52,970
So now let me go ahead and make the request again.

46
00:03:53,450 --> 00:03:57,590
And you can see password has been changed successfully.

47
00:03:57,590 --> 00:04:00,950
So let me add y to it as successfully.

48
00:04:01,400 --> 00:04:05,150
So let me go ahead and log in with the updated password.

49
00:04:05,150 --> 00:04:07,760
So let's look at the log in.

50
00:04:07,850 --> 00:04:14,300
So first of all, let me log in with the format password as one, two, three, four, five and hit

51
00:04:14,300 --> 00:04:14,730
send.

52
00:04:16,510 --> 00:04:21,820
You can see we have looking for meaning that this password has been changed.

53
00:04:21,850 --> 00:04:24,540
If I change it back to one, two, three.

54
00:04:24,550 --> 00:04:25,570
Let's have a look.

55
00:04:27,120 --> 00:04:28,980
Now it works fine.

56
00:04:29,010 --> 00:04:29,670
All right.

57
00:04:29,670 --> 00:04:36,870
So in the next video, let's go ahead and allow a user to update the profile picture or the cover image.

