1
00:00:08,830 --> 00:00:10,000
In this video.

2
00:00:10,000 --> 00:00:14,860
Let's see how we can verify this generated token.

3
00:00:15,160 --> 00:00:17,290
So back to the file.

4
00:00:17,320 --> 00:00:19,330
This is what we are going to do.

5
00:00:19,690 --> 00:00:24,430
We are going to create a function to verify the token.

6
00:00:24,580 --> 00:00:28,240
So this is the function to generate token.

7
00:00:28,270 --> 00:00:32,470
The next function is going to be point number two.

8
00:00:32,500 --> 00:00:36,700
Here is going to be verify token.

9
00:00:36,700 --> 00:00:38,930
And the function goes like this.

10
00:00:38,950 --> 00:00:42,040
Token is equal to this function.

11
00:00:42,040 --> 00:00:45,310
We take in the token that we want to verify.

12
00:00:45,670 --> 00:00:53,770
So inside here we are going to make this of the package called JWT above here.

13
00:00:53,800 --> 00:00:56,620
The one that we have required.

14
00:00:57,160 --> 00:00:57,610
All right.

15
00:00:57,610 --> 00:01:07,300
And on that, we have a method called verify and this verify fixing three argument because we are dealing

16
00:01:07,300 --> 00:01:08,830
with callback here.

17
00:01:09,070 --> 00:01:14,560
We can use async await, but let's make it pretty simple here by using callback here.

18
00:01:14,620 --> 00:01:18,760
So you pass in the token that you're passing to this function.

19
00:01:18,760 --> 00:01:25,930
And then the key that was used to sign the token and the key was this one.

20
00:01:25,960 --> 00:01:28,750
The one that we use here is the key.

21
00:01:28,750 --> 00:01:30,310
The second element.

22
00:01:30,490 --> 00:01:33,970
And then third element is going to be the callback.

23
00:01:33,970 --> 00:01:42,550
And for this callback we have access to error and then the decoded user as that and then inside the

24
00:01:42,550 --> 00:01:46,210
function body, we will have access to the user.

25
00:01:46,270 --> 00:01:49,840
So let's check if there is error.

26
00:01:49,870 --> 00:01:54,220
It means that there is something wrong with the token.

27
00:01:54,400 --> 00:01:58,530
So we can quickly return from this function.

28
00:01:58,540 --> 00:02:03,550
Let's return object with some properties like this.

29
00:02:03,670 --> 00:02:11,260
We can see stateless, meaning the status of the verification is false or failed.

30
00:02:13,420 --> 00:02:13,960
Right.

31
00:02:13,960 --> 00:02:20,050
And then we can provide a message by saying invalid.

32
00:02:21,190 --> 00:02:22,240
Tolkien.

33
00:02:23,520 --> 00:02:26,460
Or token expired.

34
00:02:26,760 --> 00:02:29,640
You can give it a neat descriptive name.

35
00:02:29,850 --> 00:02:31,770
So otherwise.

36
00:02:31,890 --> 00:02:36,540
After Eve, it means that everything went on well.

37
00:02:36,540 --> 00:02:42,210
And then we can go ahead and then return the decoded user.

38
00:02:42,450 --> 00:02:43,680
All right, so here we go.

39
00:02:43,710 --> 00:02:45,090
We can return it here.

40
00:02:45,090 --> 00:02:46,980
Has decoded.

41
00:02:47,520 --> 00:02:48,180
Perfect.

42
00:02:48,180 --> 00:02:56,180
So let's go ahead and then run this function called verify and you pass in the token.

43
00:02:56,190 --> 00:02:58,410
So this what we are going to do.

44
00:02:58,530 --> 00:03:01,800
Back to the to call the token here.

45
00:03:01,830 --> 00:03:04,590
This is a function that we are trying to implement.

46
00:03:04,590 --> 00:03:08,300
Where is going to take the token and then verify.

47
00:03:08,310 --> 00:03:12,360
And that's what this function is doing over here.

48
00:03:12,570 --> 00:03:13,980
So let's see.

49
00:03:14,070 --> 00:03:20,400
Let's go ahead to the root for logging in and then let's copy this token.

50
00:03:21,420 --> 00:03:26,670
So let's get back to the two and let's see how we can verify this one here.

51
00:03:26,670 --> 00:03:35,010
If I paste it, you can see that I got back the entire user for signing in and that is it.

52
00:03:35,010 --> 00:03:41,430
And you can see that we have what is called Issue Act and Expiration Act and this is what we are going

53
00:03:41,430 --> 00:03:43,560
to get inside our code.

54
00:03:43,560 --> 00:03:51,810
So let me copy that and back to the file and let me paste here as an argument or I can assign to a variable.

55
00:03:51,840 --> 00:03:54,930
Let me paste it inside here as the argument.

56
00:03:54,930 --> 00:03:56,850
So let's save it.

57
00:03:56,850 --> 00:04:05,520
And to be able to see the decoded user less console.log from this function as decoded.

58
00:04:06,700 --> 00:04:15,430
When I save the file, let's check the terminal and you can see that we have the user, which is same

59
00:04:15,430 --> 00:04:20,950
as the one we saw when we use the tool from JWT.

60
00:04:21,279 --> 00:04:23,170
Let's look at the database here.

61
00:04:23,470 --> 00:04:25,210
We have the user.

62
00:04:25,240 --> 00:04:33,730
This user represents the user we use to sign meaning to generate this token and we got it back and we

63
00:04:33,730 --> 00:04:42,250
have two properties ie 80 which simply means issue add and this is the date that it was issued and the

64
00:04:42,250 --> 00:04:47,090
expiration ad as this one and they are in milliseconds.

65
00:04:47,110 --> 00:04:51,490
So this is how we verify a token.

66
00:04:51,490 --> 00:04:59,590
So what we are going to do is that after a token has been generated, then we are going to create a

67
00:04:59,590 --> 00:05:05,040
murderer always to check the request that is coming in.

68
00:05:05,050 --> 00:05:13,960
If a user passes this token in that way, we are going to take that token and then verify it.

69
00:05:14,140 --> 00:05:18,160
So this is how we verify a token in this video.

70
00:05:18,190 --> 00:05:26,980
Let's continue from here and see how we can continue to implement authentication system using this approach.

