1
00:00:00,750 --> 00:00:07,410
We're a little bit familiar with Mongo's law, so now let's continue with our steps, we're saving the

2
00:00:07,410 --> 00:00:09,200
user, so we're good on that.

3
00:00:09,510 --> 00:00:17,340
And of course, we just want to generate Tolkan and send back the response and we'll first set up functionality

4
00:00:17,340 --> 00:00:18,780
again in the controller.

5
00:00:18,960 --> 00:00:20,700
So we're clear on what's happening.

6
00:00:20,940 --> 00:00:26,410
And next video, I'll show you another nifty setup that we can use with Mongoose.

7
00:00:26,790 --> 00:00:32,430
And like I already previously mentioned, this should feel familiar because we spend the entire project

8
00:00:32,430 --> 00:00:34,920
on that, the JWT basics.

9
00:00:35,220 --> 00:00:41,760
So if we go back to the controllers in the project, meaning the folder name is, of course, this is

10
00:00:41,760 --> 00:00:42,660
not the first project.

11
00:00:42,900 --> 00:00:48,420
And if we take a look at the main check it out, we go with JWT sign.

12
00:00:48,420 --> 00:00:48,990
Correct.

13
00:00:49,230 --> 00:00:53,760
And of course, that is coming from our library, the and Web talking one.

14
00:00:54,180 --> 00:00:58,890
Then we pass in whatever we want to pass it on to the front end.

15
00:00:59,160 --> 00:01:05,790
And of course, later we'll use the idea to access the resources that are associated with that user.

16
00:01:06,280 --> 00:01:09,480
So then we pass in the JWT secret one.

17
00:01:09,760 --> 00:01:16,320
And in this project I'll show you where we can get a proper value and eventually pass in the options

18
00:01:16,560 --> 00:01:20,100
and we just say in how long it expires.

19
00:01:20,430 --> 00:01:24,050
So let's try to recreate that back in the jazz.

20
00:01:24,450 --> 00:01:26,000
And what are we looking for here?

21
00:01:26,280 --> 00:01:28,590
We're looking for the library, of course.

22
00:01:28,750 --> 00:01:33,060
Now I can remove the script because everything is already set up in the model.

23
00:01:33,180 --> 00:01:35,640
So let's go over here with JWT.

24
00:01:36,210 --> 00:01:43,050
We will set it equal to require, of course, and then we're looking for the package, the JSON Web

25
00:01:43,050 --> 00:01:43,620
tokens one.

26
00:01:43,920 --> 00:01:48,480
And then once we create the user, this is the case where we want to create a token.

27
00:01:48,480 --> 00:01:48,870
Correct.

28
00:01:49,170 --> 00:01:52,700
So let's go over here with Consed and then we'll go Tolkan.

29
00:01:52,740 --> 00:01:59,280
And for time being I'll just pass in some random string and then we'll set up properly everything after

30
00:01:59,280 --> 00:01:59,490
that.

31
00:01:59,760 --> 00:02:07,110
So let's go here with JWT, then we go with sign of course and we want to pass in the data we want to

32
00:02:07,110 --> 00:02:15,750
send back and again we'll go with ID and so let's start with user I.D. That's just how I decided to

33
00:02:15,750 --> 00:02:16,740
call my property here.

34
00:02:16,950 --> 00:02:19,950
So user ID is equal to user.

35
00:02:20,190 --> 00:02:22,620
So that's the user that I'm getting back over here.

36
00:02:22,920 --> 00:02:26,340
And remember, on that object, we have it property.

37
00:02:26,490 --> 00:02:26,970
Correct.

38
00:02:26,970 --> 00:02:29,520
And if you want to double check, it's right over here.

39
00:02:29,700 --> 00:02:31,530
This is what we're currently sending back.

40
00:02:31,920 --> 00:02:36,000
And we go with underscore, of course, and then we'll go with it.

41
00:02:36,690 --> 00:02:40,370
And again, that's just my preference to kind of separate it.

42
00:02:40,370 --> 00:02:44,760
Two, otherwise, you have job, you have user ID and many more ideas.

43
00:02:44,760 --> 00:02:50,670
And then if you just keep using this, underscore, in my opinion, is just hard to manage everything.

44
00:02:50,670 --> 00:02:54,630
So that's why I always go with user already, job already or whatever.

45
00:02:54,930 --> 00:02:57,880
So that's the first thing that I'm sending up next.

46
00:02:57,990 --> 00:02:59,100
I want to send the name.

47
00:02:59,550 --> 00:03:02,610
So I go with name and then again, user.

48
00:03:02,820 --> 00:03:04,530
That's my object and the name.

49
00:03:04,800 --> 00:03:09,750
And like I said, I'm just going to pass in some JWT secret right away.

50
00:03:10,170 --> 00:03:15,780
So I'm just going to be cheating a little bit over here and then we'll add a comma and then let's go

51
00:03:15,780 --> 00:03:17,760
again with expression.

52
00:03:17,890 --> 00:03:21,810
Remember, this one was expires in thirty days.

53
00:03:22,020 --> 00:03:23,190
So we use the same value.

54
00:03:23,190 --> 00:03:27,000
And of course, a little bit later I'll talk about other options as well.

55
00:03:27,270 --> 00:03:28,170
So let's go here.

56
00:03:28,290 --> 00:03:34,680
Let's say expires in and I'll set it equal to thirty days.

57
00:03:35,150 --> 00:03:36,390
Then we save.

58
00:03:36,420 --> 00:03:42,210
And of course, instead of sending back the user, I want to send back the token.

59
00:03:42,720 --> 00:03:47,430
And this is the case where as always, there's multiple setups that you can have.

60
00:03:47,880 --> 00:03:55,230
You can send back maybe only the token or you can be like, hey, maybe my front end needs the name

61
00:03:55,230 --> 00:03:55,740
directly.

62
00:03:55,980 --> 00:03:57,180
Now what am I talking about?

63
00:03:57,540 --> 00:04:05,520
If I go back to the application that I built for this project, if I go to login and of course, more

64
00:04:05,520 --> 00:04:11,040
specifically, I'm going to be looking for a register and then I'll try to use some kind of values here

65
00:04:11,580 --> 00:04:13,710
that are not already database.

66
00:04:13,710 --> 00:04:18,240
So I'm going to go with honor and maybe let's add a two, because then for sure, everything is going

67
00:04:18,240 --> 00:04:18,630
to work.

68
00:04:19,079 --> 00:04:24,780
Once I submit not only I'm going to get back token, I'm going to say here never.

69
00:04:25,110 --> 00:04:26,850
I'll also get back to name.

70
00:04:27,030 --> 00:04:31,560
But again, this really depends on the setup that you have on the front end.

71
00:04:31,950 --> 00:04:34,290
You can send back only the token.

72
00:04:34,410 --> 00:04:41,460
And actually there are setups where the front end decodes the token instead, instead of me sending

73
00:04:41,460 --> 00:04:46,170
here a name and set it equal to username front and the code the token.

74
00:04:46,470 --> 00:04:48,060
Now, it's not going to be our case.

75
00:04:48,060 --> 00:04:54,600
But just keep in mind, that's why you don't ever want to store anything here as far as the secret,

76
00:04:54,990 --> 00:04:57,570
because as far as the coding, you can actually do that.

77
00:04:57,780 --> 00:04:58,950
Now, of course, when it comes to.

78
00:04:59,250 --> 00:05:06,360
Fine, that's where you need that JWT secret, but as far as decoding, yes, there are some setups

79
00:05:06,600 --> 00:05:10,860
where fronton just the codes and get whatever property it needs.

80
00:05:10,980 --> 00:05:18,080
So in my case, the jobs up, I'm also looking for a user and more specifically the name property.

81
00:05:18,210 --> 00:05:22,950
But hopefully it is clear that in this case it's just my preference when I was building the front end

82
00:05:22,950 --> 00:05:23,190
up.

83
00:05:23,610 --> 00:05:29,760
When it comes to response, what you definitely want to send back is the token, because not, of course,

84
00:05:29,760 --> 00:05:35,390
will allow that user to access resources on a server later on.

85
00:05:35,640 --> 00:05:37,260
So I'm I guess I'm going to go with user.

86
00:05:37,360 --> 00:05:39,230
So that's going to be another property I'm sending.

87
00:05:39,480 --> 00:05:45,630
And in this case, of course, I'll access my user object and I'm just going to be looking for a name.

88
00:05:45,870 --> 00:05:53,050
So I'll have another property there by the name of name and then I'll set it equal to user and then

89
00:05:53,070 --> 00:05:53,520
name.

90
00:05:53,640 --> 00:05:58,650
And of course, now I just need to add a comma and we're successfully sending the token.

91
00:05:58,860 --> 00:06:01,380
And again, let's test that out in a postman first.

92
00:06:01,770 --> 00:06:06,290
So I'm going to go here with John three at Gmail dot com.

93
00:06:06,300 --> 00:06:06,990
We send it back.

94
00:06:06,990 --> 00:06:09,990
Now, notice we're not sending back the password anymore.

95
00:06:10,350 --> 00:06:14,370
And of course, I'm getting the user as well as the token.

96
00:06:14,670 --> 00:06:20,340
And just to reiterate on the front end, of course, I'm using down over here.

97
00:06:20,670 --> 00:06:28,290
So underreact as the state and I set up the state value just to show because that over here, my producer,

98
00:06:28,290 --> 00:06:35,340
blah, blah, blah, I have user notice the name because that is Anna and I'm accessing that right from

99
00:06:35,340 --> 00:06:36,270
the response.

100
00:06:36,600 --> 00:06:43,350
And then when it comes to the token, you'll have multiple options and one of them is going to be storing

101
00:06:43,350 --> 00:06:44,860
it in a local storage.

102
00:06:44,880 --> 00:06:50,670
Now, of course, in the local storage, not only to have the name so that when I refresh, of course,

103
00:06:50,670 --> 00:06:54,510
I can still shock us down, but I also get the token.

104
00:06:54,720 --> 00:07:01,110
So when I'll be making future requests on behalf of of course, I'll have the token.

105
00:07:01,350 --> 00:07:02,700
Hopefully it is clear.

106
00:07:02,820 --> 00:07:09,090
And now, of course, we can take a look at another solution that Mongo's provides to make our code

107
00:07:09,090 --> 00:07:09,570
cleaner.

