1
00:00:00,360 --> 00:00:07,710
Beautiful, and I would see how we can accomplish the same result with users that say and in the process

2
00:00:07,710 --> 00:00:10,650
will cover some gotchas you need to be aware of.

3
00:00:11,370 --> 00:00:17,820
And before we do anything, before we comment on this code and in my case, I'll leave it for your reference.

4
00:00:18,390 --> 00:00:22,280
What I want you to do is to go to user jayesh.

5
00:00:22,890 --> 00:00:25,710
We're looking for pre save.

6
00:00:25,740 --> 00:00:26,060
OK?

7
00:00:26,730 --> 00:00:29,040
And let's just set up a log here.

8
00:00:29,440 --> 00:00:33,930
Say, Hey there, and then I want you to go back to the post.

9
00:00:34,950 --> 00:00:38,850
And where we have that user, let's run it one more time.

10
00:00:39,260 --> 00:00:42,890
Doesn't really matter which values you set up or anything like that.

11
00:00:42,900 --> 00:00:44,760
In my case, I'm going to go back to John.

12
00:00:45,360 --> 00:00:50,160
I'll save and I'll send and clearly see that I changed the result.

13
00:00:50,790 --> 00:00:52,770
So now the name is back to John.

14
00:00:53,340 --> 00:00:54,840
But what is more interesting?

15
00:00:55,230 --> 00:00:57,480
We did not invoke Hey, there.

16
00:00:57,720 --> 00:01:03,720
We can clearly see that in the server and you'll see why this is important in the upcoming minutes.

17
00:01:04,110 --> 00:01:08,670
Let's now go to user the user controller.

18
00:01:09,450 --> 00:01:11,730
What I want is to leave this for your reference.

19
00:01:12,150 --> 00:01:17,790
So go here and I'll say I'm that user with find one an update.

20
00:01:18,770 --> 00:01:21,770
So let me just copy this one just so I can speed this up.

21
00:01:22,460 --> 00:01:26,900
And what I'm going to do is just come this one out.

22
00:01:27,650 --> 00:01:30,230
Basically, I'll copy and paste first, actually.

23
00:01:31,220 --> 00:01:32,780
Then I'll come on this one out.

24
00:01:34,830 --> 00:01:42,180
And then I'll move it down again just so you can take a look if you want, so cut it out and then right

25
00:01:42,180 --> 00:01:44,230
after the exports, I'll copy and paste.

26
00:01:44,250 --> 00:01:46,530
And let me add here a comment.

27
00:01:46,980 --> 00:01:48,240
Havana will go back.

28
00:01:48,750 --> 00:01:54,270
And before I do anything, I'm just going to say update user with user not say.

29
00:01:55,970 --> 00:02:02,300
And then as far as the set up, the initial set up, this still stays the same or we're looking for

30
00:02:02,300 --> 00:02:04,070
email, we're looking for a name.

31
00:02:04,670 --> 00:02:07,040
We're still checking one of them is missing.

32
00:02:07,280 --> 00:02:07,880
Send back.

33
00:02:07,880 --> 00:02:09,190
Please provide all values.

34
00:02:09,889 --> 00:02:16,640
Now the gotcha here is going to be that we're not using find one and update instead on one or more.

35
00:02:16,640 --> 00:02:25,130
I'll want to start everything from scratch and I'll say user is equal to await then user and in this

36
00:02:25,130 --> 00:02:26,090
case were use.

37
00:02:26,300 --> 00:02:36,410
Find one and we'll say, get me the user where the underscore ID is equal to rec user and user ID.

38
00:02:37,280 --> 00:02:44,120
And once I get, the user will manually change the values of, say, user, not email.

39
00:02:45,490 --> 00:02:47,200
Is equal to an email that we're getting.

40
00:02:47,590 --> 00:02:47,950
Correct.

41
00:02:48,070 --> 00:02:54,070
So that's equal to any email and online username is equal to any name.

42
00:02:54,520 --> 00:03:02,500
And once I'm done updating the properties, so once I get the user and I set Rickey's equal to ruin

43
00:03:02,500 --> 00:03:08,300
your hours, I also want to go with a wait user that's safe and say it.

44
00:03:08,710 --> 00:03:14,840
And now I want to go back to the postman when I John-John, that email and all of that in the update

45
00:03:14,860 --> 00:03:15,250
user.

46
00:03:15,650 --> 00:03:20,680
And let's try it out one more time where we're going to go with you, John.

47
00:03:20,860 --> 00:03:25,570
I'll send it, and I can clearly see that we update the value.

48
00:03:26,290 --> 00:03:34,780
So there's going to be some cases where you'll want to use user data safe, instead where you'll get

49
00:03:34,780 --> 00:03:35,410
the user.

50
00:03:35,920 --> 00:03:40,960
You'll update the properties manually and then you will use the save method.

51
00:03:40,990 --> 00:03:44,800
However, here comes the big gotcha when you do that.

52
00:03:45,430 --> 00:03:46,690
Notice the console.

53
00:03:47,170 --> 00:03:48,370
Notice the halo.

54
00:03:49,060 --> 00:03:50,740
So where is this coming from?

55
00:03:51,070 --> 00:03:53,730
It's coming from pre Saybrook.

56
00:03:54,070 --> 00:03:55,150
Now, what does that mean?

57
00:03:55,930 --> 00:03:58,840
Well, that means that we're messing with the password.

58
00:03:59,200 --> 00:04:00,430
And what is the result?

59
00:04:01,030 --> 00:04:06,940
Well, let's go to log in user where I know for sure that my password is secret.

60
00:04:07,450 --> 00:04:09,190
And let's try to do that one more time.

61
00:04:09,970 --> 00:04:13,670
And notice once I go, I get back invalid credentials.

62
00:04:13,690 --> 00:04:14,320
Why?

63
00:04:15,100 --> 00:04:15,940
Because of us.

64
00:04:16,690 --> 00:04:25,240
Because when we use save, we are invoking the hook and effectively we hashed the password one more

65
00:04:25,240 --> 00:04:25,570
time.

66
00:04:25,960 --> 00:04:31,260
And that's why, even though our value was correct, I mean, it was secret.

67
00:04:31,660 --> 00:04:34,810
But once we updated the user, that's it.

68
00:04:35,470 --> 00:04:36,700
We tampered with that.

69
00:04:36,820 --> 00:04:38,980
And now we cannot log in again.

70
00:04:39,520 --> 00:04:40,540
And what is the fix?

71
00:04:40,570 --> 00:04:47,710
Well, the fix is following where I want you to go back to a pretty safe analog.

72
00:04:47,710 --> 00:04:50,800
Something will log this dot, modify paths.

73
00:04:51,460 --> 00:04:55,750
So essentially, this is going to showcase what are we modifying?

74
00:04:55,930 --> 00:04:59,590
So let's go here and let's say log.

75
00:05:00,070 --> 00:05:04,060
I will say this dot modified paths.

76
00:05:04,720 --> 00:05:06,840
So notice we're talking about this.

77
00:05:06,850 --> 00:05:08,290
We're talking about the user model.

78
00:05:08,470 --> 00:05:09,520
We invoke it.

79
00:05:10,240 --> 00:05:13,540
And let's go back where we have update user now.

80
00:05:13,660 --> 00:05:16,240
We will have to create this user from scratch eventually.

81
00:05:16,240 --> 00:05:18,370
But now I just want to log stuff.

82
00:05:18,700 --> 00:05:24,330
I want to go with your John and maybe let's go with new John app Gmail account.

83
00:05:24,370 --> 00:05:25,510
Let's send.

84
00:05:26,140 --> 00:05:31,270
And now what I'm interested here is the paths that we're modifying.

85
00:05:31,810 --> 00:05:37,820
I notice here we go with email, and that's the path that we're modifying because we already had new

86
00:05:37,840 --> 00:05:38,590
John correct.

87
00:05:38,890 --> 00:05:42,970
So let me maybe set them both back to you, John.

88
00:05:43,920 --> 00:05:51,210
And Janet, gmail.com and now you'll notice an array with two hours, so now we're modifying email and

89
00:05:51,210 --> 00:05:51,570
name.

90
00:05:51,870 --> 00:05:59,420
And what's even more cool, I can specifically take a look whether the property is modified.

91
00:05:59,430 --> 00:06:00,310
How can I do that?

92
00:06:00,330 --> 00:06:06,010
Well, we can go log and then we go and arrest dot and is modified.

93
00:06:06,480 --> 00:06:09,450
That's the method modified.

94
00:06:09,630 --> 00:06:14,790
Like so and then I specifically need to say, what property am I looking for?

95
00:06:15,090 --> 00:06:20,760
Now, first, I'll just log name and then will apply that to the password.

96
00:06:21,060 --> 00:06:23,370
I don't set up a little bit of functionality.

97
00:06:23,400 --> 00:06:25,020
So for now, I just want to say name.

98
00:06:25,440 --> 00:06:30,900
So if I'm going to go back, I'm let me add new John here and let me send.

99
00:06:31,380 --> 00:06:36,420
And now you'll notice that I get back false because I'm not modifying the name.

100
00:06:37,170 --> 00:06:40,740
And essentially in the array, I can see that I'm modifying the email.

101
00:06:41,220 --> 00:06:43,560
So if we put two and two together?

102
00:06:44,530 --> 00:06:51,610
I only want to do reverse the hashing password stuff if we're modifying the password.

103
00:06:52,510 --> 00:06:59,950
So whether that is when we are creating the user or not is when we are updating the password, correct?

104
00:07:00,490 --> 00:07:02,950
If we're just updating our values.

105
00:07:03,430 --> 00:07:04,510
I don't want to do that.

106
00:07:04,840 --> 00:07:05,860
How we can set that up.

107
00:07:06,310 --> 00:07:14,110
Well, for starters, I'll leave these ones for your reference and I'm going to go with if and not this

108
00:07:14,110 --> 00:07:17,830
that is modified is modified.

109
00:07:18,130 --> 00:07:20,050
And here I want to pass in the password.

110
00:07:20,470 --> 00:07:29,110
So if we're not modifying the password, then return, then don't apply this if we're modifying the

111
00:07:29,110 --> 00:07:29,560
password.

112
00:07:29,860 --> 00:07:36,550
Essentially, when we're creating user or we're updating the password, one will apply.

113
00:07:37,060 --> 00:07:38,320
So now let's go back.

114
00:07:38,770 --> 00:07:43,780
And like I said, I'm going to have to remove John and start basically from scratch.

115
00:07:44,020 --> 00:07:45,220
Otherwise, it's not going to make sense.

116
00:07:45,230 --> 00:07:47,130
So let me go to my users.

117
00:07:47,740 --> 00:07:49,180
Let me refresh.

118
00:07:49,540 --> 00:07:54,910
I should have three, and I really don't care whether the John is admin or not.

119
00:07:55,270 --> 00:07:59,140
I can always update that manual over here in the database anyway.

120
00:07:59,440 --> 00:08:04,990
What I want to do is go back to the postman where I registered a user.

121
00:08:05,600 --> 00:08:08,770
I'll start from the scratch or go here, John and John.

122
00:08:10,440 --> 00:08:12,540
And then I'll pass in the secret.

123
00:08:12,870 --> 00:08:13,770
OK, awesome.

124
00:08:14,220 --> 00:08:15,450
And get back, my user.

125
00:08:15,780 --> 00:08:21,870
Now I want to update the user and instead of new email, I'm going to go with New John.

126
00:08:22,200 --> 00:08:23,130
I send it.

127
00:08:23,430 --> 00:08:28,950
And now if everything is correct, I should be able to log in.

128
00:08:29,340 --> 00:08:33,480
So I go back here and everything still clearly works.

129
00:08:33,929 --> 00:08:39,720
So those are the things that you need to be aware of where, yes, there's going to be some cases where

130
00:08:39,750 --> 00:08:42,630
you'll want to use save instead.

131
00:08:43,080 --> 00:08:45,750
So notice with fine one an update.

132
00:08:45,780 --> 00:08:49,500
We did not trigger the hook, but when you you save.

133
00:08:49,530 --> 00:08:51,090
Yes, you are triggering the hook.

134
00:08:51,100 --> 00:08:51,540
So.

135
00:08:52,500 --> 00:09:00,600
If you have some kind of functionality in safe, how in this case you need to make sure that it only

136
00:09:00,600 --> 00:09:02,010
runs when you want it.

