WEBVTT 00:01.890 --> 00:06.780 So we've managed to grab the authorization header and extract the token from it, and now it's time 00:06.780 --> 00:08.630 to get the user from the tokens table. 00:08.670 --> 00:14.790 So right here on line three hundred and twenty three in my code, I'm going to simply call a function 00:14.790 --> 00:16.230 that doesn't exist yet. 00:17.100 --> 00:22.550 So get a variable, which I'll call user, and I'm going to check for an error and they're going to 00:22.550 --> 00:26.370 come from a database method that we'll write in a minute after DB. 00:26.370 --> 00:35.490 Don't get user with the capital you for token and we're going to hand it the string token and we'll 00:35.490 --> 00:36.150 check for an error. 00:36.660 --> 00:42.090 If error is not equal to nil, then return nil and error or start new. 00:43.890 --> 00:48.510 No matching user found. 00:49.440 --> 00:58.800 OK, and what will return here is actually the user, which means I can get rid of this variable value 00:58.800 --> 00:59.610 that we have at the top. 01:01.110 --> 01:06.590 Now we need to go write this function, get user for token and we're going to put that in tokens dot 01:06.600 --> 01:08.580 go at the very bottom of the file. 01:10.230 --> 01:21.840 I'll create a new function func with receiver M of type div model get user with a cavalier for token 01:23.280 --> 01:30.330 and we'll have the value or the parameter t which is a string for Toker and will return a pointer to 01:30.870 --> 01:33.540 user and potentially in error. 01:36.180 --> 01:42.210 Now as I usually do, I'm going to save myself the trouble of typing this context stuff by copying it. 01:44.700 --> 01:52.020 And pasting it down here now, we're actually receiving Tolkan, which is a string that's exactly twenty 01:52.020 --> 01:56.700 six characters long, but we don't have that information stored in the database. 01:56.700 --> 01:58.310 If you go back to our database and look at. 01:59.750 --> 02:05.240 There we actually have a hash of the token and some of you might be asking yourselves the question, 02:05.240 --> 02:07.640 why don't you just store the token in the database? 02:07.670 --> 02:13.930 Well, I don't want to do that because I never want to store a valid token in the database. 02:13.940 --> 02:18.710 If that ever gets compromised or somebody gets the token from the database, then they have access to 02:18.710 --> 02:21.470 as all the users that they possibly could want. 02:21.950 --> 02:27.200 So instead, just as you never store a password in the database, you never store a token in the database 02:27.200 --> 02:27.530 either. 02:27.530 --> 02:28.940 You store a hash instead. 02:29.360 --> 02:36.380 So what we need to do now is to convert that token we're receiving in this function into a hash, which 02:36.380 --> 02:37.520 I'll call token hash. 02:40.040 --> 02:43.210 And actually I'm going to call the parameter token just because it's more readable. 02:44.630 --> 02:51.380 So token hash that's going to be sign the value of we're going to call the S.H. a two fifty six package 02:51.380 --> 02:53.990 from the crypto library, part of the standard library. 02:54.000 --> 03:00.080 So actually two fifty six dot some two fifty six, which is a method on that. 03:01.420 --> 03:10.180 And we're going to pass it a slice of bytes, which is our token, so slice of bite and that gives me 03:10.180 --> 03:15.430 my token hash now create a user variable user of type of user. 03:15.840 --> 03:17.520 So I have somewhere to store the information. 03:17.530 --> 03:19.260 And let me give myself some more room here. 03:20.080 --> 03:25.480 Now, I'll write the query and the query is assigned the value and I'll use the tactics as I usually 03:25.480 --> 03:33.100 do, and I'll type it like this select and I want to get from you, which will be an alias to the user's 03:33.100 --> 03:33.510 table. 03:33.520 --> 03:41.910 I want the users ID and the first name and last name and the other email and that should be enough. 03:42.970 --> 03:51.730 We're going to select that from users you and we're going to enter join the Tokens table, which is 03:51.730 --> 03:52.700 where our token is. 03:52.750 --> 04:01.150 Hash is stored from tokens t and we're going to join on the Utah ID equals T dot user ID. 04:01.720 --> 04:08.140 So we have the user ID stored in the tokens table as user underscore ID and that's going to match ID 04:08.170 --> 04:09.100 on the user's table. 04:10.960 --> 04:19.480 And now we do our we're close and this is where we're using our token hash, where to hash equals questionmark 04:19.480 --> 04:20.230 are placeholder. 04:20.650 --> 04:27.640 OK, now you may notice that I'm actually not checking for token expiry at this point, and we'll get 04:27.640 --> 04:28.780 to that before too long before. 04:28.790 --> 04:30.230 Right now, let's just get this working. 04:30.410 --> 04:40.510 OK, so now we'll populate the very variable error in case there's an error and we're calling m dot 04:41.050 --> 04:44.800 query row context because we know we're going to get one at most one row. 04:44.800 --> 04:49.690 And we had of the context and we handed our query and then we handed our token hash. 04:50.570 --> 04:55.110 But you can't do it like that because this is a slice and I need to convert it to an array. 04:55.120 --> 04:58.990 And the way you do that and go is a square brackets and column in the middle. 04:59.200 --> 04:59.890 Now it's an array. 05:01.350 --> 05:11.070 And we're going to in one step scan this into the variable user that I created up there online. 05:11.100 --> 05:16.020 Seventy nine percent user ID and I'll duplicate that a few times. 05:17.310 --> 05:19.380 So ID then first name. 05:21.810 --> 05:22.740 Then last name, 05:25.740 --> 05:26.310 then email. 05:28.380 --> 05:37.620 And then we check for an error, if error is not equal to nil, then a log it for right now, just in 05:37.620 --> 05:47.600 case I have a typo in my school log print line error and I'll return nil and the error. 05:48.660 --> 05:49.850 Otherwise, we have our user. 05:50.580 --> 05:53.460 So we'll return and we're returning a pointer to the users. 05:53.470 --> 05:56.640 We have to use the ampersand to make a reference anywhere. 05:58.260 --> 06:04.590 So now I have get user for Tolkan, which means if I go back to Handler's Dash API Dutko, there should 06:04.590 --> 06:05.370 be no error. 06:05.560 --> 06:08.280 OK, so this is a good start. 06:08.580 --> 06:14.030 And in the next lecture we could actually begin to try this out and see how it works.