WEBVTT 00:02.290 --> 00:03.620 So how did you make out with the challenge? 00:03.760 --> 00:07.210 Hopefully you noticed there were a few steps necessary to make this happen. 00:08.020 --> 00:10.220 Well, let me show you how I'm going to do it. 00:10.270 --> 00:12.070 I'm going to, first of all, look at my database. 00:12.220 --> 00:13.390 So let's look at our database. 00:13.960 --> 00:16.570 And you will notice that nowhere in the Tokens table. 00:16.570 --> 00:17.570 Let me look at the structure. 00:19.150 --> 00:22.360 Nowhere in the tokens table do we actually have the expiry. 00:22.360 --> 00:27.490 So we have no means of querying the database to see when this tokens might expire. 00:27.940 --> 00:29.990 So first of all, let's create some migrations. 00:30.010 --> 00:42.100 So I open my terminal and I will say so to generate FIDs and expiry two tokens and that will create 00:42.100 --> 00:43.520 my up and down migrations. 00:43.540 --> 00:51.220 So let's open those right under migration's and I'll open the up migration first and then I'll put in 00:51.220 --> 00:59.230 here is a simple line add column and I want to add it to the tokens table and I want it to be called 00:59.230 --> 01:08.260 expiry and I want it to be a timestamp and I'll give it a default. 01:09.570 --> 01:10.650 I'll give it no default at all. 01:10.710 --> 01:15.870 OK, so which means we're going to have to delete our entry from the Tobins table right now to avoid 01:15.870 --> 01:17.210 a null error, but that's no problem. 01:17.970 --> 01:24.000 And then we'll go to our down migration and that's simply drop column 01:26.610 --> 01:30.370 from the tokens table and we want to drop expiry. 01:31.740 --> 01:34.280 OK, so let's run this migration and make sure it works. 01:35.490 --> 01:37.050 So the migration. 01:39.540 --> 01:46.200 And there it is, it applied the migration and I always get this error, Wawn ignoring file schema don't. 01:46.620 --> 01:49.800 Just ignore that it serves no useful purpose. 01:49.830 --> 01:51.870 So now we should have a column in our database. 01:51.930 --> 01:52.530 Let's make sure 01:56.010 --> 01:57.000 to refresh this. 01:57.000 --> 01:58.040 And there is expiry. 01:58.080 --> 02:01.350 OK, so I have that and I have a place to put it. 02:01.350 --> 02:06.480 And while I'm at it, let me go to my tokens table and delete that one entry that's in there. 02:09.830 --> 02:10.560 Delete selected. 02:10.850 --> 02:11.360 Perfect. 02:11.520 --> 02:21.230 OK, so now let's go close these two migration's and Heider Migration's folder and let's go right to 02:21.230 --> 02:30.400 the roots for our API so API roots, so authenticate API slash, API slash authenticate. 02:30.410 --> 02:33.870 That's where we do our insertion of an entry into the database. 02:33.890 --> 02:35.980 So let's make sure we have the necessary logic in there. 02:36.260 --> 02:40.990 So I'll go to create Tolkan or create off token so we get our user input. 02:41.120 --> 02:44.740 We read the JSON, we try to get the user from the database by email. 02:44.750 --> 02:48.550 We make sure their password matches and down here we generate a token. 02:48.560 --> 02:53.620 And as you can see, the second argument to this function generate Tolkan is our expiry. 02:53.630 --> 02:55.190 We expire in twenty four hours. 02:55.760 --> 03:02.120 So I know at this point that my token has the necessary expiry because we're populating it based upon 03:02.120 --> 03:02.520 this. 03:02.550 --> 03:05.740 OK, so we then save it to the database. 03:05.780 --> 03:12.200 Here's what we have to make our first actual change other than the migration we need to insert into 03:12.200 --> 03:16.790 tokens, user ID name, email, Tolkan Hash and then the expiry. 03:18.230 --> 03:23.690 OK, which means down here after the hash I just add t dot expiry. 03:25.520 --> 03:26.180 There it is. 03:26.300 --> 03:29.360 OK, so that will inserted into the database. 03:29.660 --> 03:35.540 And of course the other change we need to make is when we're saying get user for Tolkan down here. 03:35.930 --> 03:38.570 And this is the bit we were working on in the most recent lectures. 03:38.930 --> 03:46.370 We just have to modify this to say and t dot expiry is greater than questionmark. 03:47.780 --> 03:49.100 And then we come down here. 03:51.210 --> 03:57.460 And we add time now, and that should be all we have to do. 03:57.720 --> 04:05.100 So let's try this was Clearstream and make a and we see if we got it. 04:05.100 --> 04:05.480 All right. 04:06.920 --> 04:15.950 And we'll go back to our idea and go to the homepage and I'll log in and I'm going to log in as admin 04:15.950 --> 04:22.730 at example, dot com with the password, password and log in. 04:23.720 --> 04:27.530 And we have an error column count doesn't match value count at row one? 04:29.660 --> 04:31.010 So let's see where that mistake is. 04:37.430 --> 04:42.860 One, two, three, four, five, six, one, two, three, four, five, six, seven. 04:42.890 --> 04:43.860 There it is right there. 04:44.000 --> 04:45.620 OK, let's add that questionmark. 04:47.210 --> 04:48.170 I do that all the time. 04:48.740 --> 04:52.730 Restart things, make stop, make start. 04:55.220 --> 05:05.690 Let's try this again back into our Web browser, refresh this page, OK, and log in as an example, 05:05.690 --> 05:08.630 dot com password and log in. 05:09.590 --> 05:10.730 OK, so we're logged in. 05:10.740 --> 05:14.170 We should be able to see the virtual terminal page and we can. 05:14.570 --> 05:20.720 Now, let's go back to the home page and let's go to the database and change the expiry for the record, 05:20.720 --> 05:21.700 that was just inserted. 05:21.860 --> 05:25.130 So I refresh this and I'll make the. 05:26.070 --> 05:34.140 Expiry, I'll just put it one year ago, and save that and come back here and try going to the virtual 05:34.140 --> 05:39.330 terminal page and it redirects us so expiry works the way that it should. 05:39.990 --> 05:41.520 OK, that wasn't terribly difficult. 05:41.760 --> 05:47.430 Now it's time to go work on some middleware so we can secure things like this, even though I'm logged 05:47.430 --> 05:47.670 in. 05:47.820 --> 05:52.470 If I go to admin at example, dot com and password. 05:54.120 --> 05:58.590 And again, this is great and I can now see the virtual terminal page. 05:58.800 --> 06:05.400 The problem is that the route behind this page that actually calls the back end using our JavaScript 06:05.400 --> 06:07.470 fetch statement, that's not protected. 06:07.740 --> 06:10.440 And we need to make sure that it is protected. 06:10.620 --> 06:15.750 And we'll get started on that by writing some middleware and changing some of the logic on the front 06:15.750 --> 06:17.490 end in the next lecture.