WEBVTT 00:01.350 --> 00:05.670 So we've managed to generate a token and send it to the front end and everything seems to be working 00:05.670 --> 00:10.380 really well, but of course, once we send that token at the moment, we forget about it. 00:10.390 --> 00:16.710 We have no means of looking at a token sent to us from the front end validating that it's one we issued 00:16.890 --> 00:18.930 and validating that it hasn't expired. 00:18.930 --> 00:20.650 And we'll be doing all that before too long. 00:20.910 --> 00:22.500 So obviously, we need to save it somewhere. 00:22.830 --> 00:29.820 And obviously we're going to do that right here in Handler's Dash APG go in the create auth token handler 00:30.630 --> 00:32.010 before we send the response. 00:32.010 --> 00:35.910 But after we've generated the token we'll say save to database. 00:36.270 --> 00:37.830 We're going to save the token to the database. 00:37.840 --> 00:39.830 And of course that means we need somewhere to put it. 00:40.350 --> 00:43.920 So I'll open my terminal and I will generate a migration. 00:44.040 --> 00:52.380 So to generate fears and I'll call this create tokens table and then we'll create two tokens for us 00:52.380 --> 00:53.540 or two migration's for us. 00:54.060 --> 00:58.710 So I'll open my migration's folder and as I usually do, because I don't want to have any typographical 00:58.710 --> 01:06.090 errors, I'll grab an up migration, say this one, for creating the customer's table copy and paste 01:06.090 --> 01:12.330 it into my newly created up migration and make sure you get the up migration and I'll just modify this. 01:12.390 --> 01:19.710 So I'm going to create a table called tokens and the first column will be an ID primary is true. 01:19.710 --> 01:26.280 Find the second one, I'll make the user I.D. and that will be the user's ID, which is of course an 01:26.280 --> 01:28.890 integer and it is unsigned. 01:28.920 --> 01:31.380 So let's make it unsigned again. 01:32.880 --> 01:34.910 Unsigned, true. 01:37.170 --> 01:42.750 The next one I probably want to save at least their last name, so I'll just call it name string and 01:42.750 --> 01:43.320 that's fine. 01:43.710 --> 01:45.900 And I want to save the user's email as well. 01:46.320 --> 01:47.760 And then I want to store the hash. 01:47.760 --> 01:49.980 So we'll duplicate this line and I'll leave it. 01:50.100 --> 01:51.270 I'll call it hash 01:54.540 --> 01:59.960 and it is a string right now, but it actually needs to be storing a slice of bytes. 02:00.000 --> 02:00.930 That's what our hashes. 02:01.000 --> 02:01.640 That's not a problem. 02:02.280 --> 02:05.860 What I do, first of all, is modify these two SQL statements to set the default. 02:05.920 --> 02:07.320 Now for the timestamps. 02:07.560 --> 02:11.370 So the table is tokens and the table is tokens. 02:13.620 --> 02:20.730 After I generate the table, these three are these two SQL statements, we'll get executed and I'll 02:20.730 --> 02:25.220 just add one here and modify it so it's not going to be setting a default. 02:25.230 --> 02:32.730 Instead, I'll just alter table tokens and I'll say modify hash I think hashes a reserved word. 02:32.730 --> 02:41.100 So I'll call it a token token hash and I'll set the verb binary with the length of two hundred and fifty 02:41.100 --> 02:41.430 five. 02:42.300 --> 02:54.600 And that means token hash here gets put up here, token hash again token hash that was stored down migration 02:54.870 --> 02:58.860 which is simple drop table tokens. 03:00.450 --> 03:06.840 And if everything went well I should be able to clear the screen here and say sota migrate and I'll, 03:06.860 --> 03:10.080 let's go look at the database and make sure we have everything in the right format. 03:10.080 --> 03:11.850 So this is our database. 03:11.850 --> 03:14.670 Let's refresh this and look at tokens. 03:15.090 --> 03:18.600 So we have ID for our very, very binary. 03:18.600 --> 03:19.160 Looks good. 03:19.320 --> 03:26.400 OK, so we'll close that and now we're ready to actually write some database functionality because we 03:26.400 --> 03:27.540 want to insert the tokens. 03:27.540 --> 03:34.740 So I'll close my two migrations and I'll go over to I'll go to tokens dot because that's in the models 03:34.740 --> 03:37.110 package so I can create a new function here. 03:37.700 --> 03:44.130 And because I'm in the topin in the models package, I can use the receiver m pointer to DB model. 03:44.560 --> 03:48.060 I'll call this insert token and it will take two arguments. 03:48.570 --> 03:49.860 The first one will be the token. 03:49.950 --> 03:55.740 I'll just call that T and it's a pointer to token again token. 03:56.910 --> 04:00.660 And the second one is the user we're going to get the information from and I'll just call that you for 04:00.660 --> 04:01.460 user at. 04:01.470 --> 04:05.790 Potentially this will return an error and I'll get rid of that extra parentheses. 04:06.810 --> 04:13.170 OK, now as I often do, I'm going to open the models Dargo file and copy this context stuff because 04:13.170 --> 04:17.490 I invariably typed that incorrectly and I'll just paste that in here. 04:17.940 --> 04:25.380 And that means I need to import context up here context and I should make the error go away or one of 04:25.380 --> 04:25.800 errors. 04:26.280 --> 04:28.140 But right now, let's create our statement. 04:28.830 --> 04:36.300 So we'll say statement is assign the value of insert into tokens and we're going to be putting in the 04:36.300 --> 04:42.720 user already their name and email hash, which I called hash. 04:42.870 --> 04:44.430 What do they call the token hash? 04:45.760 --> 04:48.540 Then I make sure hash, token or token hash. 04:50.280 --> 04:52.500 I called it token hash, so I'll copy that. 04:54.780 --> 05:06.480 Token ash and then just created and updated and we're going to be inserting the values of one, two, 05:07.740 --> 05:13.350 one, two, three, four, five, six. 05:14.040 --> 05:17.040 So one, two, three, four, five, six. 05:17.040 --> 05:19.530 One, two, three, four, five, six. 05:19.570 --> 05:19.970 Perfect. 05:21.180 --> 05:22.160 So that's our statement. 05:22.170 --> 05:28.020 Now, we'll just execute our statement so we don't care about the result, but we do care about the 05:28.020 --> 05:35.790 error or assign the value of from the receiver m db exact context and we're going to find out the context 05:36.450 --> 05:43.020 and we're going to hand it our statement and then all of the values for our placeholders, which are 05:43.200 --> 05:51.470 for the first one we're going to put in the user user last name, the user email. 05:52.500 --> 05:55.710 Then we're going to put in the hash from the token hash. 05:56.640 --> 06:00.910 And I can just put time up now and time down now. 06:02.680 --> 06:04.110 OK, we'll check for an error. 06:05.760 --> 06:12.750 If fair is not equal to nil, return the error, otherwise return nil. 06:14.490 --> 06:21.150 Now, if that all was written properly, I can go back to Handler's apart and actually just save information 06:21.150 --> 06:21.810 to the database. 06:22.200 --> 06:23.630 So we're checking for an error. 06:23.790 --> 06:30.720 Error is equal from app to insert token and hand it our token, which I call token and our user which 06:30.720 --> 06:31.350 are called user. 06:32.340 --> 06:40.620 And we'll just copy this error checking, paste this in here, open our terminal, clear the screen, 06:41.040 --> 06:44.130 make a stop to make sure nothing is running Mixter. 06:47.580 --> 06:55.770 Now, it should be able to go back to my Web browser, reload this page and put in my values that mean 06:55.800 --> 07:04.500 at example, dot com password log in, we got no error. 07:04.500 --> 07:07.790 And we have Tolkan for admin at example, dot com created. 07:07.800 --> 07:09.880 Let's go look at our database and see if it's actually there. 07:10.380 --> 07:12.460 So when the tokens table, there it is. 07:12.630 --> 07:14.550 So we have our hash, we have our email. 07:14.550 --> 07:16.530 We have everything we seem to need. 07:16.950 --> 07:17.430 Perfect. 07:18.540 --> 07:24.930 So now we need to do something with that token on the front end and we need to set up some authentication 07:24.930 --> 07:30.140 middleware on the backend for protected routes and we'll get started on that in the next election.