1
00:00:08,119 --> 00:00:15,920
In this video, let's implement a logic to verify a token that a user will send to our server.

2
00:00:15,950 --> 00:00:20,430
So let's see how we are going to implement that inside the utils.

3
00:00:20,450 --> 00:00:24,260
Let's create one file called verify token.

4
00:00:26,420 --> 00:00:28,100
And here we go.

5
00:00:28,130 --> 00:00:30,800
So you're going to use the same functionality here.

6
00:00:30,800 --> 00:00:32,890
So we have been copied this one.

7
00:00:32,900 --> 00:00:35,600
And now back to the verification token.

8
00:00:35,600 --> 00:00:43,820
We can paste it here and let's change this one to verify token.

9
00:00:43,940 --> 00:00:48,890
And here we are going to pass in the token that we want to verify.

10
00:00:48,890 --> 00:00:57,380
So here the function we are going to pass in the token and pass in here as the agreement to the function

11
00:00:57,380 --> 00:01:01,430
for this one are going to use a method called verify.

12
00:01:01,550 --> 00:01:10,160
And the next argument is not this expiration, but instead a callback function that hosts the actual

13
00:01:10,160 --> 00:01:10,910
user.

14
00:01:10,910 --> 00:01:16,670
So here you pass in the callback function and for this we have access to error.

15
00:01:16,670 --> 00:01:18,530
And then the decoded.

16
00:01:18,530 --> 00:01:24,830
When we say the code, it is simply means that the user who signed in the token, we are going to have

17
00:01:24,830 --> 00:01:26,870
the user property on that.

18
00:01:26,870 --> 00:01:30,020
So here let's use a function here.

19
00:01:30,020 --> 00:01:33,410
So let's check for error in case there is error here.

20
00:01:33,440 --> 00:01:39,260
We are going to return from this function by saying that token is not valid.

21
00:01:39,260 --> 00:01:49,370
So here we go return and we can say that token is not valid error.

22
00:01:49,700 --> 00:01:56,570
We can go ahead and then return the actual user called the decoded.

23
00:01:56,570 --> 00:01:59,510
Return the decoded user.

24
00:01:59,930 --> 00:02:02,480
We can make it as decoded user or user.

25
00:02:02,480 --> 00:02:04,010
The choice is yours.

26
00:02:04,130 --> 00:02:09,800
So in any of a route where we want to check a token, we are going to call this function.

27
00:02:09,800 --> 00:02:14,870
So let's get back to the user's root and we want to check.

28
00:02:14,870 --> 00:02:20,360
And where you want to implement the authorization is the user profile.

29
00:02:20,360 --> 00:02:24,620
So let's collapse this one and here we go for this one.

30
00:02:24,620 --> 00:02:29,360
Unless you log in and send us a token before you can go ahead.

31
00:02:29,360 --> 00:02:32,030
For the meantime, let's just verify the token.

32
00:02:32,030 --> 00:02:39,020
So let's bring in the verification token here as that the auto import is working fine.

33
00:02:39,290 --> 00:02:47,720
And inside the profile, which is this one we can call the function, they're going to pass in the token.

34
00:02:47,930 --> 00:02:57,050
So let's assign the result from this function call as result and let's console.log the result.

35
00:02:57,230 --> 00:03:03,380
So let's make the request for the meantime we need to copy the token after logging in.

36
00:03:03,380 --> 00:03:06,770
So this is the token that we are waiting to pass in.

37
00:03:06,770 --> 00:03:14,300
So here we go, user controller and our pass in here as the only agreement.

38
00:03:14,300 --> 00:03:16,340
So moment of truth.

39
00:03:16,340 --> 00:03:20,120
Let's get back to the tender client and let's locate the profile.

40
00:03:20,150 --> 00:03:28,010
So for this, let's get sent and let's check our terminal here and I'm going to see something incredible

41
00:03:28,010 --> 00:03:30,080
here and that is it.

42
00:03:30,440 --> 00:03:39,680
You can see that we have the decoded user and this is an ID of the user core item, which is this one

43
00:03:39,680 --> 00:03:47,320
when you go to log in, this is a user, let's look at the ID of the user, which is starting from six

44
00:03:47,330 --> 00:03:51,650
three and end to zero, which is equal as this one.

45
00:03:51,650 --> 00:03:52,490
Did you see that?

46
00:03:52,490 --> 00:03:56,810
So now we have decoded the actual user.

47
00:03:56,900 --> 00:04:05,030
So in case I tamper with the token, let me change something here or something here and let's hit send

48
00:04:05,030 --> 00:04:08,360
and you can see that token is not valid.

49
00:04:08,360 --> 00:04:15,020
So with this one, we are on our way to finish up with the authentication and then the authorization.

50
00:04:15,260 --> 00:04:21,050
So what you are going to do is that we are going to create a function and then that function is going

51
00:04:21,050 --> 00:04:23,960
to grab the token from the header.

52
00:04:23,960 --> 00:04:27,710
So let's see how we are going to implement that one in the next video.

