1
00:00:00,600 --> 00:00:01,080
Beautiful.

2
00:00:01,470 --> 00:00:05,130
And once you have added two variables in that envy.

3
00:00:05,850 --> 00:00:07,860
Also, make sure to restart the server.

4
00:00:08,189 --> 00:00:10,470
Sorry, I forgot to add the step.

5
00:00:10,890 --> 00:00:11,610
Previous VIDEO.

6
00:00:12,180 --> 00:00:16,440
And now we can proceed to move both functions.

7
00:00:16,880 --> 00:00:21,450
The J.W. to verify as well as the sign to UTOS folder.

8
00:00:21,810 --> 00:00:28,050
Now, why is it useful to set up those functions in there is because that way we can move them from

9
00:00:28,050 --> 00:00:29,190
project to project.

10
00:00:29,490 --> 00:00:29,880
Yes.

11
00:00:29,880 --> 00:00:36,360
Previously we also set them up in the user model and technically it's a viable option as well.

12
00:00:36,720 --> 00:00:40,380
But in this case, I want to showcase how we can do that in the UTOS folder.

13
00:00:40,740 --> 00:00:43,860
So let's create in the star a new folder.

14
00:00:44,580 --> 00:00:45,900
Call it like it is.

15
00:00:46,110 --> 00:00:48,020
Let's call it utils like.

16
00:00:48,030 --> 00:00:51,960
So let me make sure the my spelling, as always, is the best.

17
00:00:52,500 --> 00:00:53,490
It's not useless.

18
00:00:53,790 --> 00:00:57,070
It has utility and we want to create a file.

19
00:00:57,390 --> 00:00:59,370
Actually, let's create their two files right away.

20
00:00:59,670 --> 00:01:07,470
Let's go with index J.S., and this is going to make sense a little bit later, as well as JWT Jazz.

21
00:01:07,470 --> 00:01:09,120
And we want to start in the same way.

22
00:01:09,510 --> 00:01:11,100
We want to get the package.

23
00:01:11,520 --> 00:01:15,660
So let me go here and I'll remove it from the controller.

24
00:01:15,990 --> 00:01:19,860
Since all years of the functions from your anyway, let's go back there.

25
00:01:19,920 --> 00:01:27,960
Let's go to JWT Jazz and I want to create a function that creates that token for me, and I'm going

26
00:01:27,960 --> 00:01:28,770
to go with Konst.

27
00:01:29,100 --> 00:01:34,650
Create Create JWT is equal to the payload.

28
00:01:34,800 --> 00:01:39,300
So the payload is going to be the only thing that is passed then from the controller.

29
00:01:39,300 --> 00:01:40,620
So say here payload.

30
00:01:41,600 --> 00:01:48,950
And as a side note, I prefer always setting up or I shouldn't say always whenever I can, I try to

31
00:01:48,950 --> 00:01:55,400
set up my functions to accept objects, because that way I don't have to worry about the order of the

32
00:01:55,400 --> 00:01:57,370
arguments again, just my preference.

33
00:01:57,380 --> 00:01:58,250
You don't have to do that.

34
00:01:58,550 --> 00:02:00,090
Then we want to create that token.

35
00:02:00,110 --> 00:02:04,040
So let's go over here token, and let's just repeat the steps.

36
00:02:04,040 --> 00:02:09,830
We're going to go with JWT sign and we want to pass whatever payload we're getting.

37
00:02:10,759 --> 00:02:15,350
And now, since I created those variables in the area, I can access them.

38
00:02:15,560 --> 00:02:18,290
Will go with process that envy.

39
00:02:19,010 --> 00:02:22,490
And we're looking for JWT secret.

40
00:02:22,880 --> 00:02:25,520
And thirdly, let's go with expires.

41
00:02:25,520 --> 00:02:27,440
And so those are my options.

42
00:02:28,080 --> 00:02:33,490
Bipartisan panel set it equal to process process, not the envy.

43
00:02:33,710 --> 00:02:38,690
And then JWT underscore, and we're going to go with a lifetime.

44
00:02:39,080 --> 00:02:45,230
And it looked like, have a tiny bug here or move the parentheses and add them over here.

45
00:02:45,410 --> 00:02:48,880
So now this is going to be our third argument.

46
00:02:49,160 --> 00:02:52,070
We create token and we just want to return it.

47
00:02:52,490 --> 00:02:56,540
And also, let's work on that bonus one on the verify.

48
00:02:56,600 --> 00:02:57,800
So let me go back to read me.

49
00:02:58,640 --> 00:03:05,690
So we have verify function and then we want to pass into two things we want to pass in the token as

50
00:03:05,690 --> 00:03:06,620
well as the secret.

51
00:03:06,980 --> 00:03:09,980
So let's create the verify function.

52
00:03:10,460 --> 00:03:11,390
I'm going to go below.

53
00:03:11,510 --> 00:03:13,280
I'm going to call this content is.

54
00:03:14,290 --> 00:03:22,120
Duncan rallied, and this is the one that we will use in our authentication middleware, then we'll

55
00:03:22,120 --> 00:03:27,700
be looking for one argument will, well, nameless token and what will return from the function right

56
00:03:27,700 --> 00:03:32,440
away is JWT Verify and then we'll pass in the token.

57
00:03:32,860 --> 00:03:36,990
And let's go with process that land and re.

58
00:03:37,450 --> 00:03:42,490
And this is going to be JWT secret once we have both functions.

59
00:03:42,940 --> 00:03:52,570
What I want to do is go with module exports and then I'll export crude JWT and then I'll export create

60
00:03:52,570 --> 00:03:56,080
JWT as well as is token valid.

61
00:03:56,650 --> 00:04:04,990
Now you can either import them directly into the controller and everything is going to work great,

62
00:04:04,990 --> 00:04:11,790
but it's always my preference to set up in the SJS, just like we do in the years since.

63
00:04:11,800 --> 00:04:19,329
That way, I can control what I'm exporting and when I import, I can directly target things that I'm

64
00:04:19,329 --> 00:04:24,580
looking for, and I don't need to recreate those imports with multiple lines of code.

65
00:04:24,790 --> 00:04:26,500
Again, it's really up to you.

66
00:04:26,500 --> 00:04:30,550
If you don't want to use in the jazz, you don't have to just make sure that when you're importing,

67
00:04:30,970 --> 00:04:39,070
you'll have to look for create JWT or each token valve and you have to look specifically for JWT Jazz

68
00:04:39,640 --> 00:04:40,160
in this case.

69
00:04:40,180 --> 00:04:48,070
I'll also set up index where I'm going to go with Konst JWT and then we'll look for acquire.

70
00:04:48,430 --> 00:04:50,080
So that is equal to require.

71
00:04:50,740 --> 00:04:54,730
We'll look for the JWT one I'm in here.

72
00:04:55,810 --> 00:05:03,010
You know what, actually the structure of both of them, so let's create JWT, N.W.T. is token valid.

73
00:05:03,160 --> 00:05:04,480
So I got both functions.

74
00:05:04,960 --> 00:05:07,510
I'm from the in the Congress.

75
00:05:08,050 --> 00:05:09,670
I'll create one big object.

76
00:05:09,940 --> 00:05:14,800
So eventually, as we're adding more files, I'll do the same thing in the Congress.

77
00:05:14,830 --> 00:05:19,890
I'll get the contents of those files and export them directly from index jars.

78
00:05:20,260 --> 00:05:28,420
It's a node thing where basically what happens since index jazz is going to be the default import and

79
00:05:28,420 --> 00:05:31,540
we don't need to worry about setting up the proper path.

80
00:05:31,870 --> 00:05:36,010
So now we're just exporting created that way to you as well as he is talking about.

81
00:05:36,310 --> 00:05:39,130
So now back in the off controller, what I can do.

82
00:05:39,430 --> 00:05:40,070
Seconds.

83
00:05:40,660 --> 00:05:42,030
And let's just go away.

84
00:05:42,580 --> 00:05:49,720
Create JWT and we'll set it equal to require and we'll just look for utils in this case.

85
00:05:50,260 --> 00:05:52,540
So we're looking in the UTOS folder.

86
00:05:52,990 --> 00:05:53,920
Let's save it.

87
00:05:54,190 --> 00:05:57,880
Let's scroll down and we're going to remove this line of code.

88
00:05:58,330 --> 00:06:03,100
And we can just say that once we have the token user, let's create that token.

89
00:06:03,640 --> 00:06:09,790
So token is going to be equal to create JWT, and we want to pass in the payload, which is going to

90
00:06:09,790 --> 00:06:12,130
be equal to a token user.

91
00:06:12,340 --> 00:06:13,720
So once we save this?

92
00:06:14,740 --> 00:06:19,210
Let's go back to our personal and this case, I'll change that around.

93
00:06:19,600 --> 00:06:24,460
So Peter is equal to Peter Watts passing the secret.

94
00:06:24,760 --> 00:06:29,470
And if you can see the token awesome, we have successfully refactored our code.

