1
00:00:07,280 --> 00:00:08,390
In this video.

2
00:00:08,390 --> 00:00:13,480
Let's continue our authorization and then the authentication concept.

3
00:00:13,490 --> 00:00:20,450
So what we have done is authentication, meaning that a user can log in into our application.

4
00:00:20,450 --> 00:00:27,140
So like I said, HTTP servers are stateless, meaning that as soon as we look in our server does not

5
00:00:27,140 --> 00:00:29,600
remember the log in user.

6
00:00:29,690 --> 00:00:35,650
So we need to find a way to tell our server that this particular user has logged in.

7
00:00:35,660 --> 00:00:41,000
In that way we are going to use a third party package called JSON web token.

8
00:00:41,000 --> 00:00:45,950
In that way, we are going to create a token and then assign to a looking user.

9
00:00:45,950 --> 00:00:52,250
So upon every request, the user will pass that token and then our server is going to check if that

10
00:00:52,250 --> 00:00:54,200
token is valid or not.

11
00:00:54,200 --> 00:00:58,250
Because of that, let's go ahead and create a function for that.

12
00:00:58,250 --> 00:01:06,500
So in our terminal, we are going to install the package called JSON Web token and here we go and p

13
00:01:06,500 --> 00:01:19,970
m I JSON Web token and make sure that you install the exact version that is 8.5.1 and let's hit enter

14
00:01:20,390 --> 00:01:21,410
and perfect.

15
00:01:21,410 --> 00:01:22,850
Everything is correct.

16
00:01:22,850 --> 00:01:27,910
Let's clear the terminal and let's restart our server in the utils.

17
00:01:27,920 --> 00:01:31,880
Let's go ahead and then create a function to create a token.

18
00:01:31,880 --> 00:01:33,530
So here we go.

19
00:01:33,530 --> 00:01:39,440
Let's create one file called generate token dot js.

20
00:01:39,620 --> 00:01:49,850
And first is let's require the package pass JWT is equal to required and then JSON with token.

21
00:01:49,850 --> 00:01:59,480
Next is let's create the function and the function name is going to be as generate token is equal to

22
00:01:59,510 --> 00:02:06,860
for this I'm going to pass in the ID of the user that we want to assign this talking to as that and

23
00:02:06,860 --> 00:02:08,150
here we go.

24
00:02:08,330 --> 00:02:11,420
We are going to return from this function designed to.

25
00:02:11,630 --> 00:02:19,700
So on the package that is a JWT, we have the method called sign and we are going to sign with the user

26
00:02:19,700 --> 00:02:25,460
ID, we can sign with any property, but because the ID is unique, let's use that.

27
00:02:25,460 --> 00:02:28,880
And then after that we're going to pass in the key.

28
00:02:28,880 --> 00:02:35,960
So here I can say any key and we can put this one inside the environment variable.

29
00:02:36,200 --> 00:02:38,450
So here we go.

30
00:02:38,540 --> 00:02:44,300
After that we are going to pass in another configuration as expires in meaning.

31
00:02:44,300 --> 00:02:50,900
When you want to expire this token, let's say every hour you won't expire the token, meaning we want

32
00:02:50,900 --> 00:02:55,280
the user to log in back again or we can make it as ten days.

33
00:02:55,280 --> 00:02:57,080
The choice is yours.

34
00:02:57,080 --> 00:03:00,530
So now we are done with the token generation.

35
00:03:00,530 --> 00:03:02,750
So let's go ahead and then export.

36
00:03:02,750 --> 00:03:07,370
This function from this file is equal to generate token.

37
00:03:07,550 --> 00:03:13,520
So now when a user logs in, we are going to generate a token for that particular user.

38
00:03:13,520 --> 00:03:15,500
So here we go.

39
00:03:15,650 --> 00:03:24,410
Inside the user's root, let's require the function called generate token and the auto import is working

40
00:03:24,410 --> 00:03:25,160
fine.

41
00:03:25,160 --> 00:03:29,330
So our card is on from here and where we log in.

42
00:03:29,330 --> 00:03:33,800
So let me collapse this one and now we are working with the log in.

43
00:03:33,950 --> 00:03:40,940
So when a user looks in, we are going to send a token back to the user and you're going to save this

44
00:03:40,940 --> 00:03:42,800
one inside the user's browser.

45
00:03:42,800 --> 00:03:51,470
So here is the generate token and then pass in the ID as user found dot underscore ID.

46
00:03:51,500 --> 00:03:57,740
So now let's go ahead and then log in and let's see if we're going to have a token being generated for

47
00:03:57,740 --> 00:03:58,130
us.

48
00:03:58,130 --> 00:03:59,440
So here we go.

49
00:03:59,450 --> 00:04:06,980
When I look in a moment of truth and here we go, we have the token being generated.

50
00:04:07,160 --> 00:04:13,940
Well, we can also say that as soon as we register, we want to log the user in automatically.

51
00:04:13,940 --> 00:04:16,880
In that way we can also send a token.

52
00:04:16,880 --> 00:04:18,470
But I don't want that way.

53
00:04:18,500 --> 00:04:23,030
I want the user to register first and then go ahead and then look in.

54
00:04:23,240 --> 00:04:23,750
Great.

55
00:04:23,750 --> 00:04:27,170
So now we are done with the token generation.

56
00:04:27,290 --> 00:04:35,060
What has left is that we need to check the validity of the token, meaning that if a user make a request

57
00:04:35,060 --> 00:04:39,560
and send this token, this token can be tempered or it can be changed.

58
00:04:39,560 --> 00:04:43,820
So it means that any user can send any random token or server.

59
00:04:43,850 --> 00:04:47,420
In that way they're going to receive some bugs in our application.

60
00:04:47,660 --> 00:04:52,910
Because of that, JWT gives us a method to verify a token.

61
00:04:52,910 --> 00:04:55,670
So let's get into that in the next video.

