So we've managed to grab the authorization header and extract the token from it, and now it's time
to get the user from the tokens table.
So right here on line three hundred and twenty three in my code, I'm going to simply call a function
that doesn't exist yet.
So get a variable, which I'll call user, and I'm going to check for an error and they're going to
come from a database method that we'll write in a minute after DB.
Don't get user with the capital you for token and we're going to hand it the string token and we'll
check for an error.
If error is not equal to nil, then return nil and error or start new.
No matching user found.
OK, and what will return here is actually the user, which means I can get rid of this variable value
that we have at the top.
Now we need to go write this function, get user for token and we're going to put that in tokens dot
go at the very bottom of the file.
I'll create a new function func with receiver M of type div model get user with a cavalier for token
and we'll have the value or the parameter t which is a string for Toker and will return a pointer to
user and potentially in error.
Now as I usually do, I'm going to save myself the trouble of typing this context stuff by copying it.
And pasting it down here now, we're actually receiving Tolkan, which is a string that's exactly twenty
six characters long, but we don't have that information stored in the database.
If you go back to our database and look at.
There we actually have a hash of the token and some of you might be asking yourselves the question,
why don't you just store the token in the database?
Well, I don't want to do that because I never want to store a valid token in the database.
If that ever gets compromised or somebody gets the token from the database, then they have access to
as all the users that they possibly could want.
So instead, just as you never store a password in the database, you never store a token in the database
either.
You store a hash instead.
So what we need to do now is to convert that token we're receiving in this function into a hash, which
I'll call token hash.
And actually I'm going to call the parameter token just because it's more readable.
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
from the crypto library, part of the standard library.
So actually two fifty six dot some two fifty six, which is a method on that.
And we're going to pass it a slice of bytes, which is our token, so slice of bite and that gives me
my token hash now create a user variable user of type of user.
So I have somewhere to store the information.
And let me give myself some more room here.
Now, I'll write the query and the query is assigned the value and I'll use the tactics as I usually
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
table.
I want the users ID and the first name and last name and the other email and that should be enough.
We're going to select that from users you and we're going to enter join the Tokens table, which is
where our token is.
Hash is stored from tokens t and we're going to join on the Utah ID equals T dot user ID.
So we have the user ID stored in the tokens table as user underscore ID and that's going to match ID
on the user's table.
And now we do our we're close and this is where we're using our token hash, where to hash equals questionmark
are placeholder.
OK, now you may notice that I'm actually not checking for token expiry at this point, and we'll get
to that before too long before.
Right now, let's just get this working.
OK, so now we'll populate the very variable error in case there's an error and we're calling m dot
query row context because we know we're going to get one at most one row.
And we had of the context and we handed our query and then we handed our token hash.
But you can't do it like that because this is a slice and I need to convert it to an array.
And the way you do that and go is a square brackets and column in the middle.
Now it's an array.
And we're going to in one step scan this into the variable user that I created up there online.
Seventy nine percent user ID and I'll duplicate that a few times.
So ID then first name.
Then last name,
then email.
And then we check for an error, if error is not equal to nil, then a log it for right now, just in
case I have a typo in my school log print line error and I'll return nil and the error.
Otherwise, we have our user.
So we'll return and we're returning a pointer to the users.
We have to use the ampersand to make a reference anywhere.
So now I have get user for Tolkan, which means if I go back to Handler's Dash API Dutko, there should
be no error.
OK, so this is a good start.
And in the next lecture we could actually begin to try this out and see how it works.