WEBVTT 00:00.900 --> 00:05.780 So let's get started working on the back and forth on occasion, and this is going to take a while, 00:05.790 --> 00:06.640 but let's get started. 00:06.660 --> 00:10.490 And the first thing I'm going to do is create a route to a nonexistent handler. 00:11.310 --> 00:19.800 So I will come down here and create a mock post because our request from the front end is opposed and 00:19.800 --> 00:23.480 we're calling API APIs authenticate. 00:24.180 --> 00:33.930 That's the URL we put in our JavaScript Fernet and we're going to go to app to create off Tolkan, which 00:33.930 --> 00:36.540 might not create the token if the authentication doesn't pass. 00:36.780 --> 00:40.910 But that's what we're going to try to do in that handler and we don't have the handler yet. 00:40.920 --> 00:47.490 So let's go over to handlers Dash API Dutko and I'll go to the very bottom of this file and we'll create 00:47.490 --> 00:51.920 a new handler here func and it has the receiver of App Pointer to application. 00:52.080 --> 01:00.300 As always, I'm going to call this create auth token right there and we'll give it its response writer 01:03.180 --> 01:04.800 and its pointer to a request. 01:07.060 --> 01:07.410 Oops. 01:09.610 --> 01:12.030 OK, now what are we going to do in here? 01:12.750 --> 01:17.550 Well, we're actually going to be getting username and password as JSON now. 01:17.550 --> 01:24.730 I could if I wanted to actually read the request body, create a struct, read the input into it with 01:24.730 --> 01:28.920 the decoder or we're going to be doing that an awful lot, actually a great deal. 01:29.250 --> 01:35.200 So perhaps it makes sense at this point to actually call the utility to read that for us. 01:36.030 --> 01:40.290 So one of the first is create the variable we're going to pass to that utility and I'm going to do it 01:40.290 --> 01:45.360 this way over and I'll just call it user input, because this is what they're inputting and it's going 01:45.360 --> 01:46.050 to be a struct. 01:46.710 --> 01:50.160 And I'll give it to members email, which will be string. 01:50.310 --> 01:53.160 And I'll say in JSON that's going to be called email. 01:54.100 --> 01:56.850 So we're doing this in one step instead of creating a type. 01:57.900 --> 02:01.740 And we're also going to have a password, which again, will be a string. 02:02.220 --> 02:06.690 And we're going to read that from JSON and it will be called password induced. 02:07.320 --> 02:12.180 OK, so I have this and of course, that's an empty variable right now. 02:12.180 --> 02:18.390 And what I want to do next is to actually create a helper function that will allow us to read just generic 02:18.390 --> 02:18.810 JSON. 02:19.080 --> 02:25.830 We we pass some a pointer to some variable to this function along with the other things it needs. 02:26.130 --> 02:27.830 It reads it and we're done. 02:28.110 --> 02:33.390 So I'll go back to my API folder and I'll create a new file in there. 02:33.430 --> 02:41.250 I'm not going to call it Helper's because these will be helper functions and this package is main and 02:41.250 --> 02:45.360 I'll create a function here which will have the receiver of the pointer to application. 02:46.200 --> 02:50.490 And I'm going to call this read Jason JSM like that. 02:51.690 --> 02:58.890 Actually, those are all capsule's megamall caps region and it's going to take three arguments w which 02:58.890 --> 02:59.940 will be a response rate or. 03:03.360 --> 03:11.070 Which we might need her, which is the pointer to a request which we definitely need, and then we'll 03:11.070 --> 03:14.940 just have something called data, which is just going to be an interface because we don't know what's 03:14.940 --> 03:19.080 coming for the third parameter and it will potentially return an error. 03:19.680 --> 03:22.700 Now, the first thing I'll do is just put some sanity checks in place. 03:22.730 --> 03:26.580 I'm going to say I'm not going to handle anything bigger than, say, one megabyte. 03:26.580 --> 03:33.720 So I'll create a variable called Max Bytes and I'll give it the value of one zero four eight five seven 03:34.590 --> 03:35.100 six. 03:35.970 --> 03:39.140 So one megabyte, that's how many bytes we're going to allow. 03:39.870 --> 03:48.410 And now we'll take our request body and that's going to be equal to HDB Max Bytes reader. 03:49.620 --> 03:56.370 And it requires three arguments are heard on body and then Max Bytes, but that needs to be an integer. 03:56.370 --> 03:57.030 Sixty four. 03:57.030 --> 03:58.770 So we'll just cast it within sixty four. 03:59.520 --> 04:06.540 It's sixty four and we'll pass it max bytes and that will ensure we don't get some malicious user trying 04:06.540 --> 04:08.820 to pass us, pass us a massive request. 04:08.820 --> 04:15.450 But OK, now we'll create a decoder so I'll call it deck for Decoder and that's a sign of the value 04:15.450 --> 04:22.920 of Jason Dot new decoder and will hand it our body which has been sanitized to be no more than one million 04:22.920 --> 04:23.400 size. 04:23.640 --> 04:32.460 And we'll try decoding error, a sign the value of Delcourt Decode and we're going to hand it data that 04:32.460 --> 04:35.640 third parameter in our function request. 04:36.600 --> 04:38.340 So regions' third parameter. 04:38.490 --> 04:42.240 And that can be any format we want and it's going to try to decode it. 04:42.690 --> 04:45.000 And if it can't succeed, we need to hand back an error. 04:45.600 --> 04:50.700 So if error is not equal to nil, just return the error. 04:51.930 --> 04:53.720 Otherwise it's managed to decode it. 04:54.990 --> 04:57.860 Now, as strange as this might seem, we're going to decode it again. 04:58.230 --> 05:00.000 Error is equal to now. 05:00.360 --> 05:04.530 DeCourcy decode into a reference to struct. 05:06.480 --> 05:16.380 With two sets of parentheses, OK, curly braces, and I will say if error is not equal to Yossef, 05:16.800 --> 05:17.730 then we're going to return. 05:17.730 --> 05:20.430 Another error will return errors. 05:20.670 --> 05:30.570 The new Lordi must only have a single Jason value like that, OK? 05:31.560 --> 05:37.800 The reason being, I have to this has to be a you instruct the reason being I'm going to assume that 05:37.800 --> 05:43.350 any time we try to decode a JSON file that we're only going to have one entry. 05:43.350 --> 05:45.330 We don't want multiple entries in there. 05:45.330 --> 05:48.950 And I see you didn't import IO for me, so let me try that again, Io. 05:51.290 --> 05:59.240 And it didn't import errors, so let's import the errors, error errors there, and it should have imported 05:59.240 --> 06:00.180 that form and it did. 06:00.770 --> 06:03.580 So now we have a region, JSON function almost complete. 06:03.620 --> 06:05.960 All we have to do here now say return nil. 06:07.600 --> 06:13.750 And because we've actually received our data interface as a reference to a variable, we're actually 06:13.750 --> 06:20.410 just changing a pointer value and this is a really clean way to read any kind of JSON we get as a request 06:20.410 --> 06:26.020 body, assuming that that request body has only a single JSON value. 06:26.270 --> 06:28.860 OK, so now we have available to us. 06:28.900 --> 06:31.510 So let's go back to our handlers API. 06:34.850 --> 06:39.920 And let's try reading that value, so all we have to do at this point is to say error is a sign the 06:39.920 --> 06:52.220 value of APT read Jason and it requires a response writer our request and a reference to user input. 06:52.790 --> 06:57.110 And we'll check for an error if error is not equal to nil. 06:58.070 --> 07:03.620 Now, up until this point, we've been doing really basic error checking and just writing to the log 07:03.620 --> 07:04.960 and returning something like that. 07:05.300 --> 07:10.150 So let's get a little more sophisticated here now because we're actually trying to log a user in. 07:10.170 --> 07:12.410 We want to send back meaningful information. 07:13.220 --> 07:19.040 So I'm going to go back to my helper's don't go and I'm going to create another function here. 07:19.250 --> 07:20.570 Once that was an error string. 07:20.570 --> 07:22.340 You should not end with punctuation, of course. 07:24.400 --> 07:24.970 Not much better. 07:25.390 --> 07:29.410 I'm going to create another function here, but I'm going to call it bad request again, Funk with the 07:29.410 --> 07:34.780 receiver of app application and I'll just call this that request. 07:36.670 --> 07:44.940 And it will take a few arguments w which will be a response rate, response rate or two minutes. 07:48.130 --> 07:51.970 And again, the request, which is a pointer to a request. 07:52.660 --> 08:00.520 And we'll also take an error, which I'll call her of type error, and it will return potentially an 08:00.520 --> 08:00.730 error. 08:02.830 --> 08:06.530 And once again, I'll just create a payload ver I'll call it payload. 08:06.550 --> 08:14.440 This will be what we'll be sending back to the user and it will be a struct with her and it will have 08:14.460 --> 08:16.870 say error, which is a boolean. 08:16.870 --> 08:18.550 And in JSON, I'll call that. 08:20.760 --> 08:27.840 Error probably don't need that, but I'm going to put it in there and a message which will be a string 08:28.340 --> 08:31.740 and Jason, I'll call that message. 08:35.800 --> 08:43.120 Scroll up and give ourselves some room now, populate that payload with some information, so payload 08:44.560 --> 08:46.540 error is equal to true. 08:46.780 --> 08:48.910 There is an error and payload. 08:49.240 --> 08:55.750 The message is going to be equal to a third error, which returns a string, which is perfect. 08:56.770 --> 08:58.920 And now I'll just write some Jason. 08:58.930 --> 09:03.760 Now, eventually, I'm probably going to also add a write Jason function because we'll be doing this 09:03.760 --> 09:05.810 all the time, writing Jason to the end user. 09:05.830 --> 09:11.010 But right now, I'll just write it hard coded so I'll have my note and error. 09:11.170 --> 09:13.660 It's a variable I'm declaring and we will. 09:13.670 --> 09:18.970 Jason Marshall Marshall in debt just to make it a little prettier and easier to read. 09:19.600 --> 09:21.320 And we'll give it the variable payload. 09:21.700 --> 09:27.880 That's what we want to marshall with no prefix and I'll put a tab in things, OK? 09:29.230 --> 09:30.130 And we'll check for an error. 09:30.460 --> 09:36.010 If there is not equal to nil, then I'll return the error. 09:38.020 --> 09:41.200 Otherwise I'll just write this up to the end user. 09:41.200 --> 09:51.940 So w that header the set and we're going to set the content type two application JSON and we'll write 09:51.940 --> 09:52.240 it out. 09:53.020 --> 09:53.320 Right. 09:55.570 --> 09:57.580 And I will return nil because there's no error. 09:59.190 --> 10:01.560 OK, so we have this bad request I can use now. 10:02.110 --> 10:09.910 So let's go back to our handlers API and now if there's an error, I can just say after that request 10:10.180 --> 10:15.880 and give it the arguments at once, which are w are the error. 10:16.780 --> 10:19.510 And of course you have to return after that because we've got to stop at this point. 10:20.020 --> 10:23.600 But now we're sending useful information back to the user if something goes wrong. 10:25.400 --> 10:34.540 OK, now at this point, if everything's gone according to plan, I should have that user input populated 10:34.540 --> 10:36.240 with an email and a password. 10:36.340 --> 10:41.200 So what I'll do is write that back to the end user just to make sure everything works. 10:41.230 --> 10:45.180 We're not actually doing any creation of an authentication token yet. 10:45.310 --> 10:49.230 I just want to wire things up and make sure things are talking to each other as they should. 10:49.870 --> 10:53.260 So I'll create a variable which again, I'm going to call payload 10:56.980 --> 10:58.030 and that will be a struct. 11:01.710 --> 11:04.680 And I'll give it this information error, 11:07.620 --> 11:13.170 which will be a boolean, and I'll call it Jason and Jason, I'll call it error. 11:15.120 --> 11:16.470 And again, I'll just have a message. 11:18.090 --> 11:22.260 Of course, this is going to change when we're handing tokens back, but I just want to make sure things 11:22.260 --> 11:22.590 work. 11:23.490 --> 11:27.260 Jason, I'll call that message now. 11:27.270 --> 11:28.140 Populate that. 11:28.140 --> 11:33.480 So payload error equals false because nothing went wrong. 11:34.860 --> 11:37.950 And the payload message. 11:40.090 --> 11:42.390 We'll be equal to success like that. 11:44.680 --> 11:51.490 And now I'll just mercial this and write the information out to the end user, so again, OK, and I'll 11:51.490 --> 11:54.370 ignore the error in this case because I'm just making sure that things work. 11:54.790 --> 12:04.930 Jason Mercial in debt and we're going to mercial payload with no payload, with no prefix and a tab 12:06.610 --> 12:17.620 to edit it and set our header header set content type application, slash Jason 12:20.950 --> 12:21.580 and write it up. 12:22.330 --> 12:22.660 Right. 12:23.330 --> 12:23.580 Oh. 12:25.800 --> 12:32.100 So we have our handler, we have our bad request and we have a means of reading user input, which I 12:32.100 --> 12:34.170 didn't actually if I didn't read JSON right here. 12:34.590 --> 12:38.480 All of that is set up in a room, should be connected to it. 12:39.210 --> 12:40.140 And it appears to be. 12:40.690 --> 12:42.990 So let's start our application. 12:43.380 --> 12:44.040 Make start. 12:47.200 --> 12:48.740 And I'll go back to my Web browser. 12:49.420 --> 12:56.590 I will open my console JavaScript console, I'll click on log in and let's see what we get. 12:56.620 --> 12:58.390 So first of all, let's make sure our validation works. 12:58.420 --> 13:04.210 You may noticed I added a div call me six just to make this a little narrower because it's fairly large 13:04.210 --> 13:04.830 on a laptop. 13:04.840 --> 13:08.540 You can look at the template and see that in the download source when you download the source code. 13:09.340 --> 13:10.720 So let's make sure our validation works. 13:11.200 --> 13:11.650 Looks good. 13:11.800 --> 13:13.600 Now let's give it any email address. 13:13.870 --> 13:20.380 Have here dot com and the password is test and we'll watch the JavaScript console and see if we get 13:20.380 --> 13:22.950 our information displayed properly. 13:23.860 --> 13:24.580 And there it is. 13:24.760 --> 13:26.760 No error message, she says. 13:26.890 --> 13:29.860 OK, so now I know that things are are connected to each other. 13:29.860 --> 13:31.210 They're all wired up properly. 13:32.260 --> 13:34.870 So now we can start to work on actual authentication.