1
00:00:01,020 --> 00:00:08,240
And to complete our register functionality, let's learn about schema instant's methods in Mongo's,

2
00:00:08,610 --> 00:00:10,320
so I'm going to navigate to the docs.

3
00:00:10,320 --> 00:00:12,810
In this case, we're looking for schemas.

4
00:00:13,290 --> 00:00:20,250
And if we take a look at the list, we see this instance methods and essentially what happens.

5
00:00:20,640 --> 00:00:25,240
Every document we create, we can have functions on them.

6
00:00:25,740 --> 00:00:28,800
So these are going to be instances, of course, of our schema.

7
00:00:29,130 --> 00:00:29,610
Correct.

8
00:00:29,940 --> 00:00:36,990
And the way we set up those functions, we go with the scheme and then methods and then whatever function

9
00:00:36,990 --> 00:00:37,380
you want.

10
00:00:37,860 --> 00:00:45,060
So essentially, once I create that user over here in the register, that user will have a function

11
00:00:45,360 --> 00:00:50,870
and you can probably already guess that will create a function that just generates that token.

12
00:00:51,180 --> 00:00:57,510
And of course, in that function, we'll pretty much have the same code and therefore we won't have

13
00:00:57,510 --> 00:01:02,510
to bother with that in the actual controller, in the register controller.

14
00:01:02,860 --> 00:01:08,790
And let's start somewhat silly where I want to go back to my model, the user one, and let's just go

15
00:01:08,790 --> 00:01:10,130
with user schema.

16
00:01:10,890 --> 00:01:13,470
So of course, that is the name that stays the same.

17
00:01:13,720 --> 00:01:18,070
Then we go with methods and then we'll just say get name.

18
00:01:18,480 --> 00:01:23,640
So let's imagine that we're just lazy getting the user name and we have multiple options.

19
00:01:24,000 --> 00:01:28,620
We can set it up as a sink or we can just set it up as a regular function.

20
00:01:28,650 --> 00:01:31,350
So in this case, we'll just go with the regular function.

21
00:01:31,360 --> 00:01:32,910
So no async keyword.

22
00:01:33,220 --> 00:01:40,320
But again, we do want to use this function keyword, not a function, because that way this will always

23
00:01:40,680 --> 00:01:45,570
point to our document and we get name simply what do I want to do?

24
00:01:45,810 --> 00:01:49,080
I want to return this dot and name.

25
00:01:49,380 --> 00:01:55,680
And now of course, we just want to go back to auth jazz and where we have user that name, let's just

26
00:01:55,680 --> 00:02:01,100
invoke, let's say user and we're going to be looking for get a name.

27
00:02:01,250 --> 00:02:02,580
So invoke this one.

28
00:02:02,910 --> 00:02:06,820
And now let's go to the postman and let's try to send it.

29
00:02:07,230 --> 00:02:08,580
So now get back.

30
00:02:08,820 --> 00:02:09,750
I'll send it.

31
00:02:10,139 --> 00:02:14,880
And because I wasn't careful, of course I didn't pay attention to my email.

32
00:02:15,180 --> 00:02:18,780
So of course I need to change it to John for Gmail dot com.

33
00:02:19,020 --> 00:02:21,810
And I can clearly see that I still have the name.

34
00:02:22,020 --> 00:02:29,460
So if I'll change this one around on your I say Ana and maybe I'll go with and add Gmail that come,

35
00:02:30,030 --> 00:02:32,880
then I'll still be able to access the name.

36
00:02:33,030 --> 00:02:38,800
And what that means is that we can generate a token using the instance method instead.

37
00:02:38,940 --> 00:02:46,050
So again, we're trying to keep all our logic in one place where we have the user schema instead of

38
00:02:46,050 --> 00:02:49,140
scattered around our controllers.

39
00:02:49,440 --> 00:02:56,640
And let's just fix everything back and say user name will stay with that for the time being.

40
00:02:56,640 --> 00:03:02,970
Of course we can cut it out and I think retype one more time from scratch just so we can get comfortable

41
00:03:03,270 --> 00:03:04,810
with JWT.

42
00:03:04,830 --> 00:03:10,130
Now, am I going to need the import here for the token anymore so I can definitely cut it out?

43
00:03:10,350 --> 00:03:12,180
I have my user awesome.

44
00:03:12,180 --> 00:03:16,450
And eventually I'll invoke a method that will get me that token.

45
00:03:16,560 --> 00:03:20,040
So now let me save off Jass back in the user.

46
00:03:20,040 --> 00:03:22,740
Of course, the name is not going to be get name.

47
00:03:23,160 --> 00:03:26,010
And you know, I'll just set up everything from scratch.

48
00:03:26,280 --> 00:03:29,130
I'll say user schema Lanegan.

49
00:03:29,130 --> 00:03:33,750
The syntax is methods and then we're looking for the function name.

50
00:03:33,900 --> 00:03:37,350
And in this case I'll call this create JWT.

51
00:03:37,680 --> 00:03:40,350
And again, it's going to be a simple function here.

52
00:03:40,650 --> 00:03:43,770
And as far as the logic will just return right away.

53
00:03:43,770 --> 00:03:48,570
JWT sign and of course in here I need to get the library.

54
00:03:48,870 --> 00:03:54,330
So let's go at JWT at the top and then JWT signed.

55
00:03:54,540 --> 00:03:58,980
And again, remember in the function we can access the document.

56
00:03:58,980 --> 00:03:59,370
How.

57
00:03:59,580 --> 00:04:01,050
Well, by using this.

58
00:04:01,050 --> 00:04:01,500
Correct.

59
00:04:01,650 --> 00:04:08,220
And as far as the payload, again, I'm going to go with user I.D. then I'll set it equal to this and

60
00:04:08,220 --> 00:04:09,270
underscore already.

61
00:04:09,600 --> 00:04:16,140
So this points to the document and then we're going to go with comma name and then we'll just go with

62
00:04:16,320 --> 00:04:17,100
this, that name.

63
00:04:17,370 --> 00:04:24,960
And again, we'll just create a little bit where I say JWT secret and that as far as the expression

64
00:04:24,960 --> 00:04:31,860
again, we'll keep it at thirty days so expires and we'll set equal to thirty days.

65
00:04:32,100 --> 00:04:34,640
So now if I go back to all things.

66
00:04:34,690 --> 00:04:35,040
Yes.

67
00:04:35,340 --> 00:04:38,850
I simply want to call create JWT.

68
00:04:39,000 --> 00:04:39,490
That's it.

69
00:04:39,510 --> 00:04:40,710
That's all we have to do.

70
00:04:41,040 --> 00:04:48,030
So once we create the user of course we have that instant method and here I can just say token and I'm

71
00:04:48,030 --> 00:04:49,050
going to go user.

72
00:04:49,380 --> 00:04:54,720
And of course the name of the method is Create and JWT.

73
00:04:54,960 --> 00:04:59,490
We invoke it and now of course, we can go back to our postman.

74
00:05:00,090 --> 00:05:06,450
We already have one, Anna, and let's just change the name so we can see how that works, maybe with

75
00:05:06,450 --> 00:05:10,110
Peter and I'll set it here as Peter as well.

76
00:05:10,350 --> 00:05:15,630
And then once we send, of course, now I have the Peter as well as a token.

77
00:05:15,660 --> 00:05:19,710
And of course, that means that everything works successfully.

78
00:05:20,010 --> 00:05:22,890
We're done with our register controller.

79
00:05:23,100 --> 00:05:29,370
And in the process, we learned about hashing the passwords, setting up the mouse middleware, as well

80
00:05:29,370 --> 00:05:31,890
as the instance methods on a schema.

