1 00:00:01,060 --> 00:00:05,600 In the last video we put together our idea of what a track is inside of application. 2 00:00:05,620 --> 00:00:11,140 So now we can start to create a set of routes to allow a user to manipulate a track inside my routes 3 00:00:11,140 --> 00:00:11,660 directory. 4 00:00:11,670 --> 00:00:18,190 I'm gonna make a new file called Track routes dot J.S. then inside of here we're going to import or 5 00:00:18,190 --> 00:00:21,130 require in some of the same things that we got inside of all the routes. 6 00:00:21,580 --> 00:00:31,240 So at the very top I'm going to first require an express I'm going to require in Mongoose I'm going 7 00:00:31,240 --> 00:00:35,290 to require in the require auth middleware that we wrote as well. 8 00:00:35,290 --> 00:00:39,820 Chances are that we don't want the user to be able to create a track or list out any tracks unless they 9 00:00:39,820 --> 00:00:44,830 are signed in so in order to access any routes we define inside this file we're going to make sure that 10 00:00:44,830 --> 00:00:48,000 the user is signed in by using the require off middleware. 11 00:00:48,640 --> 00:00:59,290 So at the top we'll also get require off from up one directory Middleware is require us. 12 00:00:59,470 --> 00:01:04,120 Now the only reason we required in mongoose is so that we can get access to that track model that we 13 00:01:04,120 --> 00:01:05,200 just created. 14 00:01:05,200 --> 00:01:11,050 Remember if we import the track that J.S. file directly inside of multiple files we're going to accidentally 15 00:01:11,110 --> 00:01:16,090 run that line of code right there multiple times in mongoose will eventually complain and say Hey sorry 16 00:01:16,120 --> 00:01:18,800 but you can't define a track model multiple times. 17 00:01:18,940 --> 00:01:26,380 So instead to get access to that track model we'll do concert track is Mongoose dot model track like 18 00:01:26,380 --> 00:01:34,200 so however we do have to execute or require in that track file from at least one location inside of 19 00:01:34,200 --> 00:01:36,720 our project for the user dot J.S. file. 20 00:01:36,780 --> 00:01:41,940 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. 21 00:01:42,010 --> 00:01:44,270 So inside of index dot J.S. at the very top. 22 00:01:44,370 --> 00:01:49,780 Remember we required in user right there to make sure that we executed the user file at least one time. 23 00:01:50,100 --> 00:01:57,200 So I'll do that as well for the track file like so OK. 24 00:01:57,210 --> 00:02:00,260 So now we're ready to go inside of our track crowd such as file. 25 00:02:00,320 --> 00:02:07,230 So the first thing we'll do is create a new router object so I'll do a concert router is express rather 26 00:02:07,230 --> 00:02:13,180 like so I then will want to make sure that all the different routes so we attach to that router right 27 00:02:13,180 --> 00:02:16,390 there will require the user to be signed in. 28 00:02:16,390 --> 00:02:21,310 So I'm going to call router dot use require off. 29 00:02:21,760 --> 00:02:25,900 That's going to ensure that all the different request handlers that we attach to this router inside 30 00:02:25,900 --> 00:02:28,780 this file will require the user to be signed in. 31 00:02:28,930 --> 00:02:34,740 So everything else we define inside of here will it make use of the require off middleware so now we 32 00:02:34,740 --> 00:02:38,700 can start to define our first route inside of here to work with these different tracks that are going 33 00:02:38,700 --> 00:02:39,790 to be created. 34 00:02:39,930 --> 00:02:44,850 The first root handler I'm going to define will allow a user to fetch all the different tracks that 35 00:02:44,850 --> 00:02:46,600 they have ever created. 36 00:02:46,650 --> 00:02:54,050 So I'm going to make that a get method that a user can access by making a get request to slash tracks 37 00:02:55,050 --> 00:03:02,890 I'll then pass in an async callback as a second argument they'll be called with the wreck and rez objects 38 00:03:03,430 --> 00:03:08,710 it's now inside of here we want to fetch all of the different tracks that the user has created in order 39 00:03:08,710 --> 00:03:11,670 to do so we have to kind of think about two different things. 40 00:03:11,680 --> 00:03:17,320 First off we need to figure out who the current user is or more specifically what their ideas. 41 00:03:17,320 --> 00:03:22,870 Once we know that we can then attempt to make a query to our Mongo DB collection of different tracks 42 00:03:23,110 --> 00:03:28,990 through that track model that we just created now to get access to the current users I.D. or whoever 43 00:03:28,990 --> 00:03:34,320 is signed in and making a request to our application we can once again inspect that wreck object. 44 00:03:34,480 --> 00:03:39,340 Remember back inside of require off after we have successfully verified that a user provided the correct 45 00:03:39,340 --> 00:03:41,840 password something. 46 00:03:42,450 --> 00:03:46,230 Yes provided the correct by asking me you provided the correct JWT my mistake. 47 00:03:46,340 --> 00:03:52,070 After we do that we then get that user out of our database and assign them to the user property of our 48 00:03:52,070 --> 00:03:53,330 rec object. 49 00:03:53,360 --> 00:04:01,310 So in other words are user I.D. is available as rect user dot underscore I.D. so that in mind we can 50 00:04:01,310 --> 00:04:07,250 now use the track model to issue a query to our entire collection of tracks so defined all the tracks 51 00:04:07,250 --> 00:04:15,890 that have been found by this user or created by them will define tracks as a weight track dot find we 52 00:04:15,890 --> 00:04:23,600 want to find all the different tracks with a user I.D. of req dot user dot underscore I.D. like so 53 00:04:26,700 --> 00:04:27,020 all right. 54 00:04:27,070 --> 00:04:33,700 That's going to issue our query and find all the different tracks where the user I.D. is that I.D. Now 55 00:04:33,730 --> 00:04:37,420 even if we don't find any tracks we'll just get back in empty array. 56 00:04:37,420 --> 00:04:41,920 If we do find any created by this user they will all be added into this tracks variable inside of an 57 00:04:41,920 --> 00:04:46,850 array so then we can just take that away and send it back. 58 00:04:46,960 --> 00:04:52,600 So I'll do residents send tracks like so all right. 59 00:04:52,630 --> 00:04:55,960 So that's enough for our first root handler right there. 60 00:04:55,960 --> 00:05:01,900 Let's make sure that we export our router so I'll do a module that exports equals router and then we'll 61 00:05:01,900 --> 00:05:05,950 go and hook this up inside of our index dot J ust file and we can start to test it out. 62 00:05:06,560 --> 00:05:12,490 So I'm gonna go back over to index J.S. inside of here right after auth roots. 63 00:05:12,560 --> 00:05:14,710 I'll get the track roots file that we just created 64 00:05:19,090 --> 00:05:27,470 and then I'll associate that with our app object so we'll do an app that use track routes like so and 65 00:05:27,720 --> 00:05:30,340 now we're ready to test out that root handler. 66 00:05:30,440 --> 00:05:34,770 It's going to go back over to post van back over here. 67 00:05:34,870 --> 00:05:39,220 I'm going to create a new tab that we can use to get a list of all of our different tracks. 68 00:05:39,280 --> 00:05:47,900 So I'm going to make sure that I make a request to local host three thousand slash tracks and then I'll 69 00:05:47,900 --> 00:05:49,750 try making the request right away. 70 00:05:49,790 --> 00:05:53,290 Now of course I get an error immediately that says you must be logged in. 71 00:05:53,300 --> 00:05:54,910 So this actually makes a lot of sense. 72 00:05:54,920 --> 00:05:59,960 Remember we're making use of that require off middleware and at present we have not provided our Jason 73 00:05:59,960 --> 00:06:05,540 web token inside of our list of headers in order to authenticate ourselves with our API. 74 00:06:05,540 --> 00:06:07,830 We have to provide that Jason web token. 75 00:06:07,970 --> 00:06:10,900 Otherwise we're going to be turned away immediately. 76 00:06:11,270 --> 00:06:14,870 So to authenticate ourselves we can add in a header. 77 00:06:14,870 --> 00:06:21,110 So I'm going to go to the header section right here and I'll add in a header with the name of authorization 78 00:06:22,120 --> 00:06:28,850 and remember our value has to be bearer and then a space and then our Jason web token if you do not 79 00:06:28,850 --> 00:06:33,740 have your Jason web token anymore from your logged in user you can always open up another tab or go 80 00:06:33,740 --> 00:06:38,480 back to the tab that we open up previously about the signing one right here. 81 00:06:38,480 --> 00:06:42,860 So I'm gonna make another request to sign into my application to do so we'll make a post request to 82 00:06:42,860 --> 00:06:50,570 slash sign and we'll make sure that we've got a body with raw and we'll put in our email and password 83 00:06:51,910 --> 00:06:56,470 then I'll send that off and I get another Jason web token right here that I can use to authenticate 84 00:06:56,470 --> 00:06:56,920 myself. 85 00:06:57,640 --> 00:07:03,260 So once again I'm going to copy that token without the double quotes I can then go back over to that 86 00:07:03,260 --> 00:07:08,910 header we were just putting together and right after bearer we'll put in that token now once again make 87 00:07:08,910 --> 00:07:13,990 sure you've got bearer and then a space and then your token. 88 00:07:14,150 --> 00:07:19,400 Now that's going to authenticate ourselves with our Express API and now our API is going to understand 89 00:07:19,400 --> 00:07:20,750 exactly who we are. 90 00:07:21,050 --> 00:07:26,780 So we can send this request and we get a empty array which definitely makes sense because well we don't 91 00:07:26,780 --> 00:07:32,060 have any tracks yet so hopefully once we start to create some number of tracks we will see them start 92 00:07:32,060 --> 00:07:34,520 to appear when we make this request. 93 00:07:34,550 --> 00:07:35,910 Let's take another quick pause right here. 94 00:07:35,930 --> 00:07:41,270 When we come back the next video will add in the ability to create a new track inside of our track routs 95 00:07:41,270 --> 00:07:44,300 file and associate it with a very particular user. 96 00:07:44,310 --> 00:07:46,250 It's a quick pause and I'll see you in just a minute.