OK welcome back.
And the last lesson we set up all of the directories the main structure are actually file and we installed
all those important packages.
Now we're going to start some of the authors related content.
We have two main goals in this video.
We want to create the user model and we also want to configure passport to work with that user model
.
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
packages we just installed.
So the first thing I'll do is just clean this up a bit.
So we only have one VAR statement and we chain them together with comma's which is totally optional
.
We could just save var X equals require y over and over but I just like the syntax more and you'll see
it quite often.
So import passport so they're equal to require passport
Khama body parser's next.
And the order doesn't matter here.
But if you want to go in the exact order that I am are doing parser and that needs to be body Dasch
parser and then after that we're going to import something that we're going to call local strategy and
that's going to be equal to require passport Dasch local and then we have one more which is Passport
local Mangus and that's going to be able to require passport Dasch local Dasch mongoose.
OK.
And if we really wanted to we could clean this up by indenting everything over here like this making
space so that everything aligns perfectly.
Definitely just a personal preference.
I'll do it in this case so we end up with this nice little lined up require statements on the right
.
OK.
So now that we have that done everything is required.
The first thing I want to do is just try running the app make sure we don't get any issues where it
can't find a particular package you can't find a file and we don't.
So that means that everything works just fine.
Great.
The next thing I'm going to do is work on the user model.
So I'm going to make a new file inside of models called user data.
Yes there we go.
And then I'm going to open that file up
and the first thing I'm going to do instead is require mongoose var mongoose equals require mongoose
and then I'm going to define my user schema and each user has two different pieces a user name and password
and they're both strings.
So far user schema equals mongoose
equals new mongoose schema.
And we have user name colon string and password colon string as well.
So that will set up the schema and then we want to add our module that exports equals mongoose up model
and the name of the model is user singular and we're building it from the user schema just like that
and we'll save.
Now let's go back to actually yes and require this file and make sure there aren't any problems.
So an app genius will just add another require statement here and we're going to write user.
And if we really want to keep with this stylistic choice user is going to be equal to require.
And that's going to be dot slash models slash user and who need a comma.
Let's save and make sure that we can find that file.
OK
great.
Everything works fine.
So right now we have a plain user file and it has nothing to do with passport or passport local or passport
local mongoose.
But that's going to change right now.
We're going to add in the passport local mongoose to our user model and that looks like this.
First thing we need to do is imported again.
So far passport local mongoose equals require passport dash local Desch mongoose and this package makes
it really easy on us although we have to do is go down somewhere after we defined the schema and write
the single line user schema dot plugin passport local mongers what this will do is take our passport
local mongooses package that we required.
We installed it earlier in the last video and then we just required it here and it's going to add a
bunch of methods that come with that package to our user schema.
So it comes with a lot of important functionality and features that will need to use in order to have
user authentication.
And that's actually all we need to do to start.
So let's run node.
Yes.
And make sure that we don't have any errors which we don't.
Good news and we'll leave this file alone for now.
Now we'll go back to Epcot Geass and we'll start to connect some of the other packages that we installed
.
One of the first things that we need to do is tell express to use passport and we write app use passport
dot initialize just like that and then we're going to have another one right below that don't use passport
session and save.
So this code is basically setting passport up so that it will work in our application.
We need these two methods anytime we need these two lines any time we're going to use passport and we
also need to add our express session in so up top here.
I'm going to add an app to use and we actually should require express session.
But I'll show you that we can just do it like this in line express dash session which we downloaded
already and we can do it this way.
And what we need to do is run it as a function and pass in some arguments.
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
don't use on something that we're requiring and executing with some options.
And we have to pass in three different options in order for it to work with passport a secret and this
can be anything at all.
Usually it's just a few English words.
Pick your own sentence your own words.
I'm going to add in resti is the best and cutest dog in the world.
Just like that.
And the secret will be used basically to encode and decode the sessions so we're not going to be storing
data inside the session as it normally looks as English human readable data.
It's going to be encoded in this secret that we create here is going to be used to encode or to decode
that information in the session but it can be anything at all.
Ok then I mean to add into other parameters resave will set to be false and save on initialize will
also be false.
And those are just two other things that are required.
And if we leave them off we'll actually get a message that says please add them in.
So let's save and just make sure everything works.
And this is a really silly error on my part but I'm going to keep it in here rather than editing it
out because it shows an important concept which is we're trying to use apt.
The variable app app don't use we're doing something with app but it's not defined until this line.
So it's complaining to me cannot call method use of undefined.
So it's trying to call us on something or the use method on the object and the app object doesn't exist
.
So all we have to do is move this down below anywhere and we'll just do that right here.
There we go.
So we have our three act uses require the express session run the express session with these three parameters
initialized passport and run passport does session smell it safe again make sure when you run node.
Yes this time we don't get an error and we get that message.
I told you about where it would tell us that we need to have save uninitialized.
And what happened is I spelled save uninitialized strong is a long word.
So I'm just going to copy this over here and replace what we have there now for restart.
Hopefully it all goes well and we don't get any messages.
Great.
We have two more lines who we need to get set up and then we can start working on the routes.
So those two lines look like this passport serialise user user dot serialise user.
And I'll explain these in just a moment.
And then we want the same line passport that de serialise user and that should be user that de serialise
user and we need parentheses there.
OK.
So these two methods are really important on passport serialise in D.C. realize they're responsible
for reading the session taking the data from the session that's encoded and unin coding it.
That's the de serialise and then encoding it serializing it and putting it back in the session which
is what serialise user does.
And rather than us having to write our own methods serialize user and serialize user what we've done
is instead of our user judge us by adding in passport local mongoose we've added those methods in automatically
so we don't have to define user dot serialise user on our own.
We're using the one that comes with passport local mongoose and just telling passport to use what's
already defined on the user.
Same thing with dieser realize now that save and make sure that we don't have a problem there.
Everything looks good great.
We have everything set up now so that in the next video we can go and start writing the routes and the
forms and start using some of these methods.
And that's where it really gets exciting.
I'll see you in the next video