So far, the only thing we're doing in this check, authentication handler, is send back invalid credentials.
It doesn't matter what we receive from the end user, we're always sending back.
No, you're not allowed to be here in the form of Jason.
And clearly, that's not sufficient for our purposes.
So let's read a few comments determining what exactly we want to do here.
The first thing we want to do is to validate the token and get associated user.
So we're going to take the information we receive from the end user.
And that comes from based logo HTML.
This line right here, the header authorization, and that consists of the text Beera, followed by
a space followed by whatever token we've received at some point, and we're sending that token back
to the server to say, am I look at my token and see if I'm actually mean.
So we get that information from the header.
So back here, what we want to do is to call some function and I'll create a stub function right here
just above this one func app with a pointer to application and I'll call it just to authenticate Toker
authenticate token.
And it will take as a parameter just the request R, which is a pointer to HDP to request and it will
return to things potentially.
The first one is going to be a pointer to models, dot user and potentially in error.
OK, now just to make this compile, I'll just create a variable called U of type models dot user and
I'll return a reference to that and nil for the error.
OK, so we're going to validate this token and get the associated user.
And if we call this function authenticate user or authenticate token and get any kind of error at all,
that's when we send back invalid credentials.
So let's actually write that logic.
So I'll create a variable, which I'll call, say, user, and I'll check for an error and I'll call
Apte, authenticate tokens and just hand at my request.
And the request has everything that we need.
And at this point we can check to see if there's an error.
If error is not equal to nil, then we send back our invalid credentials.
So let's move that up here.
And at that point we want to stop so that returning.
But once we get past that, at this point, valid user.
OK, so let's write that logic.
When I put in here is a variable.
Same as you always payload and it's just a struct and it consists of error, which is a boolean.
And in JSON I call the error
and it also has a message which is a string.
And in JSON, I'll call that message in a populated.
So payload error equals false.
There is no error and payload message.
And here's where I'll use the user information.
OK is equal to format at this print f
and authenticated user percent s and in there I'll just put user dot email and I would just send that
information back.
App dot.
Right Jason.
And that requires the response rate and it requires the status and I'll just use hdb dot status.
OK, and the payload.
So this becomes our new version of check authentication, and this is all exactly correct, this is
all that we need here, OK?
But what we need to do up here and authenticate token is actually get that header, the one that comes
from base layout, the one that's called authorization.
Grab the token from that and do some actual validation on the token.
And of course, if that token is valid, then we have to go to the database and get the user, which
means we're going to have to go to tokens to go and write a new database method here that actually looks
up the user based upon the content that we received in that header, the token.
And if you look at the database, we can do that.
So here's our database and one user in the database, in the tokens table.
And that user is associated with user I.D. One has the last name of user.
There's the email address.
And this is the part we're going to use to find the appropriate user.
And that's the token hash.
So we'll get started on those two things, writing the database function and over.
And handlers thought API actually turning this authenticate tokens stub into a useful function that
allows us to validate tokens.
And we'll get started on that in the next lecture.