WEBVTT 00:02.140 --> 00:07.420 So we have this function generate Tolkan in the models package and it's exported, and now we need to 00:07.420 --> 00:10.790 go over to Handler's Dash API Dutko and actually use it. 00:11.440 --> 00:18.400 So right here and I mean the function to create auth token and I'm way down in my code at line 274, 00:19.300 --> 00:25.780 so let's generate a token token and potentially an error are assigned the value of from the model's 00:25.780 --> 00:26.340 package. 00:26.650 --> 00:32.170 We're going to generate a and we have to pass it a user ID and we have the user, we pulled that from 00:32.170 --> 00:32.830 the database. 00:32.860 --> 00:40.080 So user ID, we need a time to life and we'll make Rs twenty four hours so twenty four times time hours. 00:41.530 --> 00:45.520 And the last thing we need is the scope and I'm going to use the constant. 00:45.520 --> 00:51.910 We declared in the models package models dot scop authentication and we'll check for an error. 00:52.630 --> 00:54.100 If error is not equal to nil 00:57.040 --> 01:03.700 then we'll just generate a bad request app, bad request and that just requires the response writer, 01:03.700 --> 01:06.220 the reader and the error and will return. 01:06.820 --> 01:10.050 OK, otherwise we want to send this token back. 01:10.240 --> 01:16.780 So that means I'll only get a part of this payload now and a lot of cases, the only thing you want 01:16.780 --> 01:18.310 to send back is the token. 01:18.310 --> 01:23.200 But I actually want to send back the token amp'd whether or not there's been an error and the message. 01:23.200 --> 01:31.060 So I'll just add to my payload struct something called token, which is a pointer to models dot token, 01:32.170 --> 01:34.180 which is what we got back from our function. 01:36.670 --> 01:46.810 And in JSON, I will call that authentication, underscore token and close my quotes and let's format 01:46.820 --> 01:47.200 everything. 01:49.690 --> 01:55.030 And I'll actually change the message from success just to make sure that I know I have all the necessary 01:55.030 --> 01:57.450 information and to give really meaningful information back. 01:57.460 --> 02:08.200 I'll call the format package as print F and I'll say token four percent s created and I'll substitute 02:08.410 --> 02:17.590 user input or email for my percentages and I believe that will almost take care of it, except that 02:17.590 --> 02:20.350 if I don't put the token in there, a little token 02:23.440 --> 02:26.380 is equal to token and we're not actually sending back the token. 02:26.410 --> 02:27.640 So that would be kind of pointless. 02:28.300 --> 02:29.400 All right, let's give this a try. 02:29.410 --> 02:30.400 So I'll open my terminal. 02:31.310 --> 02:32.650 I'll make sure everything is stocked. 02:33.100 --> 02:34.630 It is like start. 02:37.830 --> 02:43.650 Everything started up let's go back to our Web browser, refresh this page to make sure it's current 02:44.130 --> 02:50.760 or clear the JavaScript console and all, first of all, log in with invalid credentials so invalid 02:52.620 --> 02:58.530 and invalid dot com with some random password, I should not be able to log in. 02:59.100 --> 03:06.480 And I got invalid authentication credentials and I'll put in my one valid user admin, that example 03:06.480 --> 03:11.700 dot com with the password, password, all lowercase and I should get a token back. 03:13.230 --> 03:18.900 So here we are, object error, false messages token and we have an authentication token. 03:18.900 --> 03:23.400 And if we look at the authentication token, you can see that has an expiry set. 03:23.400 --> 03:24.810 Twenty four hours in the future. 03:25.140 --> 03:27.250 And there is my token. 03:27.510 --> 03:28.990 So this seems to work really well. 03:29.550 --> 03:34.550 Now, clearly, there's some more things we need to do when we generate that token in our handler. 03:34.560 --> 03:36.510 We actually need to save it to the database. 03:36.990 --> 03:39.460 And there's a few other things we need to take care of as well. 03:39.810 --> 03:44.070 So since we're saving it to the database, we'll have to have somewhere to put it, which means we have 03:44.070 --> 03:48.240 to generate a migration or two and create some models and so forth. 03:48.240 --> 03:50.850 But we'll get started on that in the next lecture.