1
00:00:00,180 --> 00:00:01,140
That was nice and easy.

2
00:00:01,560 --> 00:00:06,270
And now let's work on a more proper functionality, more socially.

3
00:00:06,780 --> 00:00:09,570
I also look for JWT.

4
00:00:09,810 --> 00:00:15,990
So remember our Jason Web token and eventually we'll use the verify function.

5
00:00:16,350 --> 00:00:21,030
Now, first, let me showcase the steps that we're going to take to get to it.

6
00:00:21,450 --> 00:00:27,270
So in here again, I'm looking for my authorization header and I'm just going to add one more condition

7
00:00:27,660 --> 00:00:30,840
and essentially not only we want to check for header.

8
00:00:31,840 --> 00:00:40,420
But I also want the commission where I'll say, if the offender does not start with Bear, so if it

9
00:00:40,420 --> 00:00:46,030
doesn't start with Bear Bryant again, we throw the authentication out and if we pass the commission,

10
00:00:46,390 --> 00:00:53,050
then we want to split the string because remember the end of this offender as it's coming in, it is

11
00:00:53,050 --> 00:00:53,470
a string.

12
00:00:53,890 --> 00:00:55,030
And we want to split it.

13
00:00:55,980 --> 00:00:57,060
On the space.

14
00:00:57,090 --> 00:00:57,420
Why?

15
00:00:57,450 --> 00:01:03,570
Well, because we have barer and then space and then a token, and then I want to select the second

16
00:01:03,570 --> 00:01:07,230
value, meaning essentially with Split will turn into an array.

17
00:01:07,650 --> 00:01:14,160
And then since arrays are zero in next best, the value zero will be bearer and token is actually going

18
00:01:14,160 --> 00:01:16,980
to be the second value with the index of one.

19
00:01:17,340 --> 00:01:24,360
And then once I grab the token, this is where we use the JWT Verify function, where the first thing

20
00:01:24,360 --> 00:01:26,730
that the function is looking for is the token.

21
00:01:27,000 --> 00:01:32,160
And the second one is not secret, which is sitting in our case in the process adopted.

22
00:01:32,520 --> 00:01:37,410
So remember when we created the token, we used to sign the JWT seeker one.

23
00:01:37,680 --> 00:01:42,990
So now, once we need to verify, actually, we'll have to use it again and then we get back the payload.

24
00:01:43,020 --> 00:01:44,190
Now what is the payload?

25
00:01:44,220 --> 00:01:46,110
Well, that is what we're passing in.

26
00:01:46,530 --> 00:01:50,760
So in this case, we have here this user I.D. and then this I.D..

27
00:01:50,910 --> 00:01:54,510
So this is the object that we're getting back in the payload.

28
00:01:54,900 --> 00:01:58,740
And essentially, I just want to pass it on to the next middleware.

29
00:01:59,070 --> 00:02:04,860
But before I do anything, I want to go to a request and add a user property.

30
00:02:05,100 --> 00:02:07,080
And this will be equal to that object.

31
00:02:07,620 --> 00:02:13,860
So then in my request, I can always access that user I.D. And there's million awareness how you can

32
00:02:13,860 --> 00:02:14,250
ride that.

33
00:02:14,550 --> 00:02:19,050
And I'll showcase some of them during the video and we go here with next.

34
00:02:19,080 --> 00:02:27,000
Now, if this fails, so if let's say there's a issue with the token, for example, it might be expired

35
00:02:27,360 --> 00:02:32,820
or I don't know, somebody tampered with the data, then we do the same thing like we're doing over

36
00:02:32,820 --> 00:02:33,920
here in the commission.

37
00:02:34,290 --> 00:02:36,330
Essentially, we throw the error.

38
00:02:36,630 --> 00:02:38,160
Let's start working on that.

39
00:02:38,610 --> 00:02:41,850
And the first thing that we want to do is import the or team.

40
00:02:42,130 --> 00:02:42,380
Correct.

41
00:02:42,810 --> 00:02:45,540
So let's go here and then let's say import.

42
00:02:46,500 --> 00:02:50,880
The import JWT from and then we're looking for Jason Worth Talking Library.

43
00:02:51,730 --> 00:02:55,370
Let's keep on moving and like I said, not only I want to check for the header.

44
00:02:55,650 --> 00:03:01,260
I also want to check whether the header starts with a bar.

45
00:03:01,440 --> 00:03:03,900
And if it doesn't, then of course we throw the error.

46
00:03:04,230 --> 00:03:11,160
So let's say here after that and that starts with now this one is a JavaScript function, so it's not

47
00:03:11,160 --> 00:03:14,260
like it's coming with node or the that we take.

48
00:03:14,280 --> 00:03:17,250
This is built into JavaScript, where it can go it starts with.

49
00:03:17,250 --> 00:03:18,270
And what is the value?

50
00:03:18,570 --> 00:03:20,760
Well, in my case, I'm going to be looking forward to better.

51
00:03:21,180 --> 00:03:24,600
So if that is the case when we throw the error.

52
00:03:24,930 --> 00:03:31,710
Now, if everything is correct, we'll get back our token so I can simply go here with a token is equal

53
00:03:31,710 --> 00:03:33,320
to all header.

54
00:03:33,330 --> 00:03:35,400
And like I said, we'll use the split method.

55
00:03:35,520 --> 00:03:42,060
So now we're turning into an array and then we'll say, let's let that string on the empty space and

56
00:03:42,060 --> 00:03:46,560
then let me grab it right away, the second value in the array, which will be my token.

57
00:03:46,680 --> 00:03:48,510
And then let's set up the try and get your gun.

58
00:03:48,930 --> 00:03:50,040
So let's go, try catch.

59
00:03:50,040 --> 00:03:54,540
I'll move the next inside of the tribe lock and I'll copy this line of code.

60
00:03:54,960 --> 00:04:00,480
Essentially, the federal new error and set it up here in the catch because again, the idea is going

61
00:04:00,480 --> 00:04:01,380
to be exactly the same.

62
00:04:01,770 --> 00:04:06,720
So once I have try and catch in place, once I successfully can get the token.

63
00:04:07,020 --> 00:04:08,160
Now let's verify it.

64
00:04:08,460 --> 00:04:16,680
So we're going to go here with const and payload now will be equal to my JWT, then verify function

65
00:04:16,680 --> 00:04:18,540
like I said, and it's looking for two things.

66
00:04:18,839 --> 00:04:25,560
The token as well as process dot entry and JWT secret.

67
00:04:25,740 --> 00:04:29,310
So JWT and then underscore secret.

68
00:04:29,880 --> 00:04:34,050
And if everything is correct for the time being, I just want to lock the payload.

69
00:04:34,350 --> 00:04:36,690
I want to showcase what do we have over there?

70
00:04:36,900 --> 00:04:38,490
So let me say this again.

71
00:04:38,490 --> 00:04:43,350
We need to navigate back to the postman and we need to properly set up the Badger token.

72
00:04:44,020 --> 00:04:44,940
It's looking for a token.

73
00:04:44,940 --> 00:04:48,690
Let me just double check whether token is present here looks about right.

74
00:04:48,690 --> 00:04:51,850
And then once we send, we have update user.

75
00:04:52,230 --> 00:04:53,940
So it looks like everything worked.

76
00:04:54,420 --> 00:04:57,630
Now we just need to check what we have here in the console.

77
00:04:58,080 --> 00:05:04,680
And if I take a look notice, I have user ID and then I have the expression.

78
00:05:04,740 --> 00:05:09,540
So when the token is going to expire, as well as when it was issued, I noticed that.

79
00:05:09,780 --> 00:05:12,300
Of course, this is done using the milliseconds.

80
00:05:12,330 --> 00:05:14,520
That's why we have those massive values.

81
00:05:15,090 --> 00:05:21,420
Now, if you want, you can set it up this way where you go with record users equal to payload because

82
00:05:21,420 --> 00:05:24,240
essentially at the end of the day, we'll be looking for this user in the anyway.

83
00:05:24,450 --> 00:05:32,130
Like I said, this user I.D. now will be used in any of the controllers that will rely on that user.

84
00:05:32,460 --> 00:05:38,110
So for example, if I want to look up all the jobs for the user when I'm going to use the user I.D.

85
00:05:38,460 --> 00:05:43,370
if I want to update the user info, I mean, again, I'll use the user already.

86
00:05:43,380 --> 00:05:44,490
So hopefully that is clear.

87
00:05:44,760 --> 00:05:46,410
So I can definitely do it this way.

88
00:05:46,650 --> 00:05:49,440
You can just set it equal to this object.

89
00:05:49,830 --> 00:05:53,010
Remember that you have these two properties or you can do it this way.

90
00:05:53,010 --> 00:05:59,820
Where on copy and paste I'll come into south and it's actually my preference to basically go with user

91
00:05:59,820 --> 00:06:00,060
ID.

92
00:06:00,390 --> 00:06:04,680
And then that one is equal to a payload and user ID.

93
00:06:05,160 --> 00:06:06,360
Maybe a little extra.

94
00:06:06,600 --> 00:06:10,440
I mean, again, we can pass in the payload, but that's actually my preference.

95
00:06:10,770 --> 00:06:14,220
So I'll leave the read just so you can see what are your options?

96
00:06:14,670 --> 00:06:20,820
And then I want to do the same thing in my office where I'll navigate back and I just want to set up

97
00:06:20,820 --> 00:06:22,290
my record user.

98
00:06:22,290 --> 00:06:26,430
So this one will be where this one will be on the request object.

99
00:06:26,820 --> 00:06:27,420
And then.

100
00:06:28,370 --> 00:06:32,840
In those controllers, for example, update user will access it.

101
00:06:33,380 --> 00:06:40,320
We'll take a look at the Iraq homework and also, hey, get me the User ID property and go next.

102
00:06:40,370 --> 00:06:47,120
We pass it on to the controller and then in here we throw the error if we cannot verify the token.

103
00:06:47,390 --> 00:06:50,480
Now, I don't think I'm going to go nuclear as far as testing.

104
00:06:50,930 --> 00:06:57,980
Essentially, what I'm trying to say is I'm not going to go with every possible error as far as showcase

105
00:06:57,980 --> 00:06:59,210
what is going to be responsible.

106
00:06:59,930 --> 00:07:05,030
I think I'm just going to try to sign with and without token like we didn't already previously.

107
00:07:05,390 --> 00:07:13,880
But what I want to do is go to my controller of the off controllers and that in here where we have update

108
00:07:13,880 --> 00:07:18,740
user, I do want to showcase that we always, always have access to the user.

109
00:07:18,950 --> 00:07:26,090
So if you go here with log and go with that user, if everything is correct, if we are passing in the

110
00:07:26,090 --> 00:07:30,590
token, this will log the user in the update user.

111
00:07:30,740 --> 00:07:32,480
So let's navigate to the postman.

112
00:07:32,720 --> 00:07:37,220
I have the header with Mr. Bennett here and then that's the user.

113
00:07:37,220 --> 00:07:40,970
So that is what we're going to use later in our controller.

114
00:07:41,270 --> 00:07:48,290
And as you can see, since we have our token and have no issues, therefore we pass it on to the next

115
00:07:48,290 --> 00:07:48,730
middleware.

116
00:07:48,800 --> 00:07:56,420
But if I go to my user model and I remember that when we were creating the token we used expires in

117
00:07:56,780 --> 00:08:01,130
and I purposely did not change this in the data entry because I don't want to restart everything.

118
00:08:01,670 --> 00:08:07,460
Remember, every time you make some changes in data entry, you have to do that in this case are again,

119
00:08:07,460 --> 00:08:10,450
I'll just copy and paste and let's change this around.

120
00:08:10,460 --> 00:08:18,320
And if I remember correctly, if you just pass here the value, then no one will be treated in milliseconds

121
00:08:18,320 --> 00:08:18,890
by default.

122
00:08:19,220 --> 00:08:25,640
So if we go here with, for example, 100, well, this should be hundred milliseconds and as a result

123
00:08:25,640 --> 00:08:28,250
of this, one should expire very, very quickly.

124
00:08:28,550 --> 00:08:31,850
So let's try this one out where I'm going to go back to my logging user.

125
00:08:32,090 --> 00:08:33,500
I want to get the new token.

126
00:08:34,159 --> 00:08:37,850
That's a crucial part, because now this value also will be here different.

127
00:08:38,150 --> 00:08:43,520
And then if we go right now to the update user, we should get back the 401.

128
00:08:43,760 --> 00:08:44,780
And of course we do.

129
00:08:44,870 --> 00:08:50,390
Again, that is going to be a response if our token has already expired.

130
00:08:50,630 --> 00:08:53,110
So I don't think I'll leave this one for your reference.

131
00:08:53,120 --> 00:08:56,300
I mean, you can kind of see how everything works on.

132
00:08:56,510 --> 00:08:58,400
I'm going to send it back to my lifetime one.

133
00:08:58,730 --> 00:09:04,760
And with this in place now, I can start setting up the update user controller.

