In the last video we put together our idea of what a track is inside of application.
So now we can start to create a set of routes to allow a user to manipulate a track inside my routes
directory.
I'm gonna make a new file called Track routes dot J.S. then inside of here we're going to import or
require in some of the same things that we got inside of all the routes.
So at the very top I'm going to first require an express I'm going to require in Mongoose I'm going
to require in the require auth middleware that we wrote as well.
Chances are that we don't want the user to be able to create a track or list out any tracks unless they
are signed in so in order to access any routes we define inside this file we're going to make sure that
the user is signed in by using the require off middleware.
So at the top we'll also get require off from up one directory Middleware is require us.
Now the only reason we required in mongoose is so that we can get access to that track model that we
just created.
Remember if we import the track that J.S. file directly inside of multiple files we're going to accidentally
run that line of code right there multiple times in mongoose will eventually complain and say Hey sorry
but you can't define a track model multiple times.
So instead to get access to that track model we'll do concert track is Mongoose dot model track like
so however we do have to execute or require in that track file from at least one location inside of
our project for the user dot J.S. file.
We did that at the very top of index dot J.S. let's make sure with that we take care of that right now.
So inside of index dot J.S. at the very top.
Remember we required in user right there to make sure that we executed the user file at least one time.
So I'll do that as well for the track file like so OK.
So now we're ready to go inside of our track crowd such as file.
So the first thing we'll do is create a new router object so I'll do a concert router is express rather
like so I then will want to make sure that all the different routes so we attach to that router right
there will require the user to be signed in.
So I'm going to call router dot use require off.
That's going to ensure that all the different request handlers that we attach to this router inside
this file will require the user to be signed in.
So everything else we define inside of here will it make use of the require off middleware so now we
can start to define our first route inside of here to work with these different tracks that are going
to be created.
The first root handler I'm going to define will allow a user to fetch all the different tracks that
they have ever created.
So I'm going to make that a get method that a user can access by making a get request to slash tracks
I'll then pass in an async callback as a second argument they'll be called with the wreck and rez objects
it's now inside of here we want to fetch all of the different tracks that the user has created in order
to do so we have to kind of think about two different things.
First off we need to figure out who the current user is or more specifically what their ideas.
Once we know that we can then attempt to make a query to our Mongo DB collection of different tracks
through that track model that we just created now to get access to the current users I.D. or whoever
is signed in and making a request to our application we can once again inspect that wreck object.
Remember back inside of require off after we have successfully verified that a user provided the correct
password something.
Yes provided the correct by asking me you provided the correct JWT my mistake.
After we do that we then get that user out of our database and assign them to the user property of our
rec object.
So in other words are user I.D. is available as rect user dot underscore I.D. so that in mind we can
now use the track model to issue a query to our entire collection of tracks so defined all the tracks
that have been found by this user or created by them will define tracks as a weight track dot find we
want to find all the different tracks with a user I.D. of req dot user dot underscore I.D. like so
all right.
That's going to issue our query and find all the different tracks where the user I.D. is that I.D. Now
even if we don't find any tracks we'll just get back in empty array.
If we do find any created by this user they will all be added into this tracks variable inside of an
array so then we can just take that away and send it back.
So I'll do residents send tracks like so all right.
So that's enough for our first root handler right there.
Let's make sure that we export our router so I'll do a module that exports equals router and then we'll
go and hook this up inside of our index dot J ust file and we can start to test it out.
So I'm gonna go back over to index J.S. inside of here right after auth roots.
I'll get the track roots file that we just created
and then I'll associate that with our app object so we'll do an app that use track routes like so and
now we're ready to test out that root handler.
It's going to go back over to post van back over here.
I'm going to create a new tab that we can use to get a list of all of our different tracks.
So I'm going to make sure that I make a request to local host three thousand slash tracks and then I'll
try making the request right away.
Now of course I get an error immediately that says you must be logged in.
So this actually makes a lot of sense.
Remember we're making use of that require off middleware and at present we have not provided our Jason
web token inside of our list of headers in order to authenticate ourselves with our API.
We have to provide that Jason web token.
Otherwise we're going to be turned away immediately.
So to authenticate ourselves we can add in a header.
So I'm going to go to the header section right here and I'll add in a header with the name of authorization
and remember our value has to be bearer and then a space and then our Jason web token if you do not
have your Jason web token anymore from your logged in user you can always open up another tab or go
back to the tab that we open up previously about the signing one right here.
So I'm gonna make another request to sign into my application to do so we'll make a post request to
slash sign and we'll make sure that we've got a body with raw and we'll put in our email and password
then I'll send that off and I get another Jason web token right here that I can use to authenticate
myself.
So once again I'm going to copy that token without the double quotes I can then go back over to that
header we were just putting together and right after bearer we'll put in that token now once again make
sure you've got bearer and then a space and then your token.
Now that's going to authenticate ourselves with our Express API and now our API is going to understand
exactly who we are.
So we can send this request and we get a empty array which definitely makes sense because well we don't
have any tracks yet so hopefully once we start to create some number of tracks we will see them start
to appear when we make this request.
Let's take another quick pause right here.
When we come back the next video will add in the ability to create a new track inside of our track routs
file and associate it with a very particular user.
It's a quick pause and I'll see you in just a minute.