1
00:00:00,480 --> 00:00:05,370
All right, and now let's knock out the update user controller as well.

2
00:00:05,910 --> 00:00:09,120
But in this case, I'll show you two approaches.

3
00:00:09,900 --> 00:00:17,100
First is going to be with find one and update, and a second one will be with a user save.

4
00:00:17,580 --> 00:00:24,540
And you'll see in the process why I'm showing you both setups and effectively two steps you need to

5
00:00:24,540 --> 00:00:25,530
take our following.

6
00:00:25,560 --> 00:00:32,400
First, I want you to add authenticate user middleware to their out so that user.

7
00:00:33,350 --> 00:00:39,350
Then we want to check for name and password, because like I already said before, this is the case

8
00:00:39,350 --> 00:00:42,300
where we have separate functionality.

9
00:00:42,320 --> 00:00:46,340
We have one for a password over here.

10
00:00:46,970 --> 00:00:55,910
And when it comes to updating user details, we want to be very specific where we want to update details

11
00:00:56,510 --> 00:00:59,030
that are not going to mess up our authentication.

12
00:00:59,570 --> 00:01:07,580
For example, I don't want to be user so correct that a one user two update from the admin or I'm sorry

13
00:01:07,580 --> 00:01:08,900
from the user to the admin.

14
00:01:09,320 --> 00:01:10,340
Hopefully, that makes sense.

15
00:01:10,670 --> 00:01:16,560
So therefore, in our case will just check for two things in the body for name and email.

16
00:01:16,910 --> 00:01:20,330
So both of them need to be present if one of them is missing.

17
00:01:20,570 --> 00:01:24,650
Then we go with 400 and then we'll use Find One and update.

18
00:01:24,860 --> 00:01:33,290
Remember, every user is going to be in record user online and we want to create a token and attach

19
00:01:33,530 --> 00:01:34,010
cookies.

20
00:01:34,280 --> 00:01:34,640
Correct.

21
00:01:34,820 --> 00:01:35,240
Why?

22
00:01:35,540 --> 00:01:40,340
Well, because now we're changing some values so frontend needs to know about it.

23
00:01:40,790 --> 00:01:42,410
So if I go from John.

24
00:01:43,380 --> 00:01:48,990
To Susan, it would be very helpful that the front end would know that right away.

25
00:01:49,200 --> 00:01:52,200
So then we can change the hello and whatever.

26
00:01:52,590 --> 00:01:54,900
So let's start working on that.

27
00:01:55,320 --> 00:02:01,650
And first, I'm going to navigate their use, their controller or not actually jump the gun.

28
00:02:01,980 --> 00:02:03,970
First, I want to go to use their roots.

29
00:02:04,690 --> 00:02:10,169
Remember, we need to go with grandma and an old girl with authenticate user.

30
00:02:10,610 --> 00:02:14,250
That's the first step in the user controller.

31
00:02:15,000 --> 00:02:16,590
We'll look for a route.

32
00:02:16,590 --> 00:02:18,750
So that's our app, that user one.

33
00:02:18,960 --> 00:02:22,590
And like I said, first, I want to check for email.

34
00:02:22,990 --> 00:02:27,000
This is going to be the case where both of them need to be there.

35
00:02:27,840 --> 00:02:33,810
And before you wondering, well, whether that is a very realistic setup, please keep in mind that

36
00:02:34,200 --> 00:02:40,110
when we set up the jobs API and a front end, I showed you that when it comes to edit.

37
00:02:40,770 --> 00:02:45,960
So this is going to get that one specific item, but as far as editing.

38
00:02:46,950 --> 00:02:49,930
You are sending a request with all the items.

39
00:02:50,040 --> 00:02:51,600
That's a pretty typical setup.

40
00:02:52,500 --> 00:02:58,320
Where you already have all the current items, and if you don't want to change the company or the position,

41
00:02:58,500 --> 00:03:06,270
the only want to change the status, you'll still have these ones as they are already in a database.

42
00:03:06,300 --> 00:03:07,350
So hopefully that makes sense.

43
00:03:07,380 --> 00:03:13,500
That's why we clearly can say, yeah, we're looking for all the values in our user control in our case

44
00:03:14,070 --> 00:03:15,090
email and name.

45
00:03:15,120 --> 00:03:15,690
Why?

46
00:03:16,290 --> 00:03:19,620
Because in the models, I have other things as well.

47
00:03:20,010 --> 00:03:25,080
So I don't want the user to change the password because we are to have separate for that.

48
00:03:25,650 --> 00:03:28,140
And the same goes for all I don't want.

49
00:03:28,140 --> 00:03:30,540
Like I said the user to go from user to admin.

50
00:03:30,960 --> 00:03:33,700
Now does frontend restrict that?

51
00:03:33,720 --> 00:03:34,710
Yes, of course it does.

52
00:03:34,720 --> 00:03:39,090
But in general, you don't want to have that type of security holes.

53
00:03:39,510 --> 00:03:43,110
Therefore, we will go back to user control before email and name.

54
00:03:43,320 --> 00:03:46,260
So those are two properties if one is missing.

55
00:03:46,440 --> 00:03:49,170
So same deal if email is not there or.

56
00:03:50,140 --> 00:03:51,880
The name is not there one.

57
00:03:51,890 --> 00:03:54,550
When do we do we throw back the era?

58
00:03:55,490 --> 00:04:00,890
And one more thing, yes, of course, normally your checked right away for that on the front then.

59
00:04:00,890 --> 00:04:04,970
But since we're just focusing on the back end, we want to be very diligent.

60
00:04:04,970 --> 00:04:11,660
So we'll go throw new them or not, and we're going to go with that request.

61
00:04:12,350 --> 00:04:16,220
So please provide all values when we want to update the user.

62
00:04:16,220 --> 00:04:17,480
So we're going to go with Konst.

63
00:04:17,480 --> 00:04:26,880
User is equal to a weight and not user and then find one, find one and update now.

64
00:04:26,900 --> 00:04:33,260
We'll be looking for user who's I.D. matches the one that is on the record user.

65
00:04:33,530 --> 00:04:36,610
So let's go with underscore the rack.

66
00:04:37,310 --> 00:04:40,640
And we're going to go with user user I.D..

67
00:04:41,060 --> 00:04:44,110
Remember that was on our user object.

68
00:04:44,150 --> 00:04:45,560
Then we'll add a comma.

69
00:04:46,160 --> 00:04:47,410
We're looking for two values.

70
00:04:47,420 --> 00:04:48,980
We're looking for email and name.

71
00:04:49,130 --> 00:04:53,210
And lastly, we do want to add the options were all, say, neutral.

72
00:04:53,480 --> 00:04:55,130
So return a new one.

73
00:04:55,550 --> 00:05:01,190
And also, we want to run the validators say, yep, we also want to do that one.

74
00:05:01,340 --> 00:05:03,380
Like I said, we need to create that token.

75
00:05:03,830 --> 00:05:11,270
We need to create that token user one more time because now our values have changed for email and name.

76
00:05:11,810 --> 00:05:16,280
And technically, we're only adding your name correct?

77
00:05:17,220 --> 00:05:23,100
But it's still important, it's still important for us front and to know whether we have a new name.

78
00:05:23,640 --> 00:05:25,950
So therefore, we'll create that token user.

79
00:05:26,340 --> 00:05:31,680
And this is the case where we do need to export this, or I'm sorry, I guess import more correctly,

80
00:05:32,490 --> 00:05:36,060
create token user is equal to require.

81
00:05:37,410 --> 00:05:39,450
And we're looking forward to Utils.

82
00:05:40,610 --> 00:05:43,790
And since we're exporting US index, we're good to go.

83
00:05:44,600 --> 00:05:45,530
So let's go back.

84
00:05:45,980 --> 00:05:48,140
We're looking here for the token user.

85
00:05:49,260 --> 00:05:57,700
Spoken user is equal to create token user, and we want to pass in the user after that.

86
00:05:58,050 --> 00:05:59,400
We want to attach the cookies.

87
00:05:59,760 --> 00:06:01,110
So let's go back up.

88
00:06:02,160 --> 00:06:06,570
I don't say at least your response over here.

89
00:06:07,090 --> 00:06:08,430
If that's correct, the response.

90
00:06:08,940 --> 00:06:10,920
And here we want to go with arrest.

91
00:06:11,430 --> 00:06:12,660
So that's going to be our response.

92
00:06:13,110 --> 00:06:17,290
And we also want to go to a user and token user.

93
00:06:17,310 --> 00:06:19,620
And lastly, we want to send back the response.

94
00:06:19,620 --> 00:06:26,370
So say that status status codes and we'll go with OK.

95
00:06:27,000 --> 00:06:32,580
And let's also set up JSON and user will be equal to a token user.

96
00:06:33,060 --> 00:06:39,850
We save this before we go to the postman and test it out between the retailers.

97
00:06:39,870 --> 00:06:44,280
I've noticed that there's a little bit of a mess up here.

98
00:06:44,910 --> 00:06:53,640
So in the logging, I was copying pasting the original status, and I paste that as created right over

99
00:06:53,640 --> 00:06:53,910
here.

100
00:06:54,840 --> 00:07:00,240
It's a better setup if it's just OK, because we're not creating a new resource, not a big deal, but

101
00:07:00,690 --> 00:07:02,700
still better to fix it.

102
00:07:03,060 --> 00:07:05,820
So now let's navigate to our postmen.

103
00:07:06,360 --> 00:07:08,070
And here I have three users.

104
00:07:08,070 --> 00:07:10,590
I have John Hewson and Peter.

105
00:07:11,010 --> 00:07:18,660
So let me log in as John looks like our create user works just fine, the create user token function

106
00:07:18,660 --> 00:07:19,140
we set up.

107
00:07:19,650 --> 00:07:21,930
So now let's see how we can update.

108
00:07:22,500 --> 00:07:26,130
So now let's go here to update user.

109
00:07:27,160 --> 00:07:30,490
And what I'm looking for here are some new viewers.

110
00:07:31,030 --> 00:07:36,550
So instead of name, I'm going to go with you and we'll send it.

111
00:07:36,670 --> 00:07:37,810
So let's go over here.

112
00:07:38,230 --> 00:07:41,740
We have your name and everything works awesome.

113
00:07:42,280 --> 00:07:45,460
So if you see the same results, we're in good shape.

114
00:07:45,880 --> 00:07:54,400
We set up the first option that we have for updating the user using Find one and update method.

115
00:07:54,790 --> 00:08:00,190
And next video we'll talk about another approach that we can take, and I'll cover the gotchas that

116
00:08:00,190 --> 00:08:01,210
we need to be aware of.

