1
00:00:00,990 --> 00:00:02,480
OK welcome back.

2
00:00:02,660 --> 00:00:08,220
And the last lesson we set up all of the directories the main structure are actually file and we installed

3
00:00:08,220 --> 00:00:10,200
all those important packages.

4
00:00:10,200 --> 00:00:13,130
Now we're going to start some of the authors related content.

5
00:00:13,560 --> 00:00:15,120
We have two main goals in this video.

6
00:00:15,300 --> 00:00:20,370
We want to create the user model and we also want to configure passport to work with that user model

7
00:00:20,390 --> 00:00:20,920
.

8
00:00:21,420 --> 00:00:26,430
So let's begin I'm going to go back to my APP JS to start and I'm just going to require all of those

9
00:00:26,430 --> 00:00:28,680
packages we just installed.

10
00:00:28,680 --> 00:00:31,950
So the first thing I'll do is just clean this up a bit.

11
00:00:32,310 --> 00:00:38,430
So we only have one VAR statement and we chain them together with comma's which is totally optional

12
00:00:38,430 --> 00:00:38,460
.

13
00:00:38,460 --> 00:00:45,030
We could just save var X equals require y over and over but I just like the syntax more and you'll see

14
00:00:45,030 --> 00:00:46,320
it quite often.

15
00:00:46,380 --> 00:00:51,570
So import passport so they're equal to require passport

16
00:00:54,150 --> 00:00:57,440
Khama body parser's next.

17
00:00:58,190 --> 00:00:59,440
And the order doesn't matter here.

18
00:00:59,460 --> 00:01:05,880
But if you want to go in the exact order that I am are doing parser and that needs to be body Dasch

19
00:01:06,000 --> 00:01:14,670
parser and then after that we're going to import something that we're going to call local strategy and

20
00:01:14,670 --> 00:01:25,050
that's going to be equal to require passport Dasch local and then we have one more which is Passport

21
00:01:27,060 --> 00:01:36,110
local Mangus and that's going to be able to require passport Dasch local Dasch mongoose.

22
00:01:36,610 --> 00:01:37,460
OK.

23
00:01:37,530 --> 00:01:45,630
And if we really wanted to we could clean this up by indenting everything over here like this making

24
00:01:45,630 --> 00:01:48,960
space so that everything aligns perfectly.

25
00:01:48,960 --> 00:01:51,950
Definitely just a personal preference.

26
00:01:52,380 --> 00:01:58,920
I'll do it in this case so we end up with this nice little lined up require statements on the right

27
00:01:58,930 --> 00:01:59,400
.

28
00:01:59,810 --> 00:02:00,090
OK.

29
00:02:00,120 --> 00:02:02,840
So now that we have that done everything is required.

30
00:02:02,850 --> 00:02:07,380
The first thing I want to do is just try running the app make sure we don't get any issues where it

31
00:02:07,380 --> 00:02:11,600
can't find a particular package you can't find a file and we don't.

32
00:02:11,610 --> 00:02:14,180
So that means that everything works just fine.

33
00:02:14,220 --> 00:02:15,110
Great.

34
00:02:15,150 --> 00:02:18,200
The next thing I'm going to do is work on the user model.

35
00:02:18,510 --> 00:02:23,050
So I'm going to make a new file inside of models called user data.

36
00:02:23,220 --> 00:02:25,600
Yes there we go.

37
00:02:25,830 --> 00:02:27,410
And then I'm going to open that file up

38
00:02:30,660 --> 00:02:38,940
and the first thing I'm going to do instead is require mongoose var mongoose equals require mongoose

39
00:02:39,840 --> 00:02:46,560
and then I'm going to define my user schema and each user has two different pieces a user name and password

40
00:02:46,770 --> 00:02:48,200
and they're both strings.

41
00:02:48,480 --> 00:02:52,890
So far user schema equals mongoose

42
00:02:56,220 --> 00:03:00,810
equals new mongoose schema.

43
00:03:01,320 --> 00:03:09,620
And we have user name colon string and password colon string as well.

44
00:03:09,960 --> 00:03:17,100
So that will set up the schema and then we want to add our module that exports equals mongoose up model

45
00:03:18,180 --> 00:03:25,110
and the name of the model is user singular and we're building it from the user schema just like that

46
00:03:25,320 --> 00:03:26,780
and we'll save.

47
00:03:26,910 --> 00:03:32,020
Now let's go back to actually yes and require this file and make sure there aren't any problems.

48
00:03:32,040 --> 00:03:38,950
So an app genius will just add another require statement here and we're going to write user.

49
00:03:39,870 --> 00:03:46,100
And if we really want to keep with this stylistic choice user is going to be equal to require.

50
00:03:46,440 --> 00:03:52,930
And that's going to be dot slash models slash user and who need a comma.

51
00:03:53,400 --> 00:03:55,980
Let's save and make sure that we can find that file.

52
00:03:55,980 --> 00:03:56,360
OK

53
00:04:00,000 --> 00:04:00,660
great.

54
00:04:00,660 --> 00:04:02,370
Everything works fine.

55
00:04:02,400 --> 00:04:08,640
So right now we have a plain user file and it has nothing to do with passport or passport local or passport

56
00:04:08,670 --> 00:04:09,880
local mongoose.

57
00:04:10,170 --> 00:04:12,360
But that's going to change right now.

58
00:04:12,360 --> 00:04:18,390
We're going to add in the passport local mongoose to our user model and that looks like this.

59
00:04:18,570 --> 00:04:20,590
First thing we need to do is imported again.

60
00:04:20,730 --> 00:04:37,440
So far passport local mongoose equals require passport dash local Desch mongoose and this package makes

61
00:04:37,440 --> 00:04:43,800
it really easy on us although we have to do is go down somewhere after we defined the schema and write

62
00:04:43,800 --> 00:04:55,980
the single line user schema dot plugin passport local mongers what this will do is take our passport

63
00:04:55,990 --> 00:04:58,260
local mongooses package that we required.

64
00:04:58,300 --> 00:05:03,040
We installed it earlier in the last video and then we just required it here and it's going to add a

65
00:05:03,040 --> 00:05:07,120
bunch of methods that come with that package to our user schema.

66
00:05:07,120 --> 00:05:11,950
So it comes with a lot of important functionality and features that will need to use in order to have

67
00:05:11,950 --> 00:05:13,160
user authentication.

68
00:05:13,420 --> 00:05:15,560
And that's actually all we need to do to start.

69
00:05:16,000 --> 00:05:17,860
So let's run node.

70
00:05:17,920 --> 00:05:18,420
Yes.

71
00:05:18,520 --> 00:05:21,430
And make sure that we don't have any errors which we don't.

72
00:05:21,430 --> 00:05:24,940
Good news and we'll leave this file alone for now.

73
00:05:24,940 --> 00:05:29,450
Now we'll go back to Epcot Geass and we'll start to connect some of the other packages that we installed

74
00:05:29,450 --> 00:05:30,330
.

75
00:05:30,630 --> 00:05:40,360
One of the first things that we need to do is tell express to use passport and we write app use passport

76
00:05:41,150 --> 00:05:50,350
dot initialize just like that and then we're going to have another one right below that don't use passport

77
00:05:51,940 --> 00:05:54,760
session and save.

78
00:05:55,480 --> 00:05:59,600
So this code is basically setting passport up so that it will work in our application.

79
00:05:59,620 --> 00:06:05,680
We need these two methods anytime we need these two lines any time we're going to use passport and we

80
00:06:05,680 --> 00:06:10,530
also need to add our express session in so up top here.

81
00:06:10,600 --> 00:06:16,380
I'm going to add an app to use and we actually should require express session.

82
00:06:16,600 --> 00:06:24,160
But I'll show you that we can just do it like this in line express dash session which we downloaded

83
00:06:24,180 --> 00:06:26,680
already and we can do it this way.

84
00:06:26,950 --> 00:06:32,420
And what we need to do is run it as a function and pass in some arguments.

85
00:06:32,560 --> 00:06:37,960
So it looks a little wonky to you but this is how we can do it in one swoop where we're doing an app

86
00:06:37,960 --> 00:06:42,840
don't use on something that we're requiring and executing with some options.

87
00:06:43,060 --> 00:06:49,450
And we have to pass in three different options in order for it to work with passport a secret and this

88
00:06:49,450 --> 00:06:50,760
can be anything at all.

89
00:06:50,760 --> 00:06:52,710
Usually it's just a few English words.

90
00:06:52,780 --> 00:06:55,060
Pick your own sentence your own words.

91
00:06:55,090 --> 00:07:02,030
I'm going to add in resti is the best and cutest dog in the world.

92
00:07:02,080 --> 00:07:03,220
Just like that.

93
00:07:03,720 --> 00:07:10,720
And the secret will be used basically to encode and decode the sessions so we're not going to be storing

94
00:07:10,720 --> 00:07:15,970
data inside the session as it normally looks as English human readable data.

95
00:07:16,120 --> 00:07:22,660
It's going to be encoded in this secret that we create here is going to be used to encode or to decode

96
00:07:22,810 --> 00:07:26,300
that information in the session but it can be anything at all.

97
00:07:26,760 --> 00:07:35,590
Ok then I mean to add into other parameters resave will set to be false and save on initialize will

98
00:07:35,590 --> 00:07:37,320
also be false.

99
00:07:37,540 --> 00:07:39,610
And those are just two other things that are required.

100
00:07:39,670 --> 00:07:43,730
And if we leave them off we'll actually get a message that says please add them in.

101
00:07:43,990 --> 00:07:47,480
So let's save and just make sure everything works.

102
00:07:47,560 --> 00:07:52,330
And this is a really silly error on my part but I'm going to keep it in here rather than editing it

103
00:07:52,330 --> 00:07:56,990
out because it shows an important concept which is we're trying to use apt.

104
00:07:57,000 --> 00:08:02,340
The variable app app don't use we're doing something with app but it's not defined until this line.

105
00:08:02,620 --> 00:08:06,910
So it's complaining to me cannot call method use of undefined.

106
00:08:06,910 --> 00:08:13,450
So it's trying to call us on something or the use method on the object and the app object doesn't exist

107
00:08:13,450 --> 00:08:14,460
.

108
00:08:14,500 --> 00:08:20,380
So all we have to do is move this down below anywhere and we'll just do that right here.

109
00:08:20,710 --> 00:08:21,540
There we go.

110
00:08:21,580 --> 00:08:28,600
So we have our three act uses require the express session run the express session with these three parameters

111
00:08:29,560 --> 00:08:37,040
initialized passport and run passport does session smell it safe again make sure when you run node.

112
00:08:37,090 --> 00:08:40,670
Yes this time we don't get an error and we get that message.

113
00:08:40,670 --> 00:08:45,330
I told you about where it would tell us that we need to have save uninitialized.

114
00:08:45,550 --> 00:08:50,970
And what happened is I spelled save uninitialized strong is a long word.

115
00:08:51,120 --> 00:08:57,830
So I'm just going to copy this over here and replace what we have there now for restart.

116
00:08:58,870 --> 00:09:01,930
Hopefully it all goes well and we don't get any messages.

117
00:09:01,920 --> 00:09:03,210
Great.

118
00:09:03,420 --> 00:09:07,590
We have two more lines who we need to get set up and then we can start working on the routes.

119
00:09:07,680 --> 00:09:19,650
So those two lines look like this passport serialise user user dot serialise user.

120
00:09:20,000 --> 00:09:21,880
And I'll explain these in just a moment.

121
00:09:22,140 --> 00:09:32,620
And then we want the same line passport that de serialise user and that should be user that de serialise

122
00:09:33,070 --> 00:09:36,840
user and we need parentheses there.

123
00:09:37,330 --> 00:09:37,690
OK.

124
00:09:37,720 --> 00:09:43,090
So these two methods are really important on passport serialise in D.C. realize they're responsible

125
00:09:43,090 --> 00:09:48,500
for reading the session taking the data from the session that's encoded and unin coding it.

126
00:09:48,610 --> 00:09:54,300
That's the de serialise and then encoding it serializing it and putting it back in the session which

127
00:09:54,310 --> 00:09:55,990
is what serialise user does.

128
00:09:56,320 --> 00:10:02,410
And rather than us having to write our own methods serialize user and serialize user what we've done

129
00:10:02,400 --> 00:10:09,420
is instead of our user judge us by adding in passport local mongoose we've added those methods in automatically

130
00:10:09,580 --> 00:10:15,570
so we don't have to define user dot serialise user on our own.

131
00:10:15,690 --> 00:10:22,150
We're using the one that comes with passport local mongoose and just telling passport to use what's

132
00:10:22,140 --> 00:10:23,610
already defined on the user.

133
00:10:23,620 --> 00:10:30,050
Same thing with dieser realize now that save and make sure that we don't have a problem there.

134
00:10:30,120 --> 00:10:32,830
Everything looks good great.

135
00:10:33,000 --> 00:10:37,500
We have everything set up now so that in the next video we can go and start writing the routes and the

136
00:10:37,500 --> 00:10:40,390
forms and start using some of these methods.

137
00:10:40,960 --> 00:10:42,470
And that's where it really gets exciting.

138
00:10:42,690 --> 00:10:43,570
I'll see you in the next video
