So we have this function generate Tolkan in the models package and it's exported, and now we need to
go over to Handler's Dash API Dutko and actually use it.
So right here and I mean the function to create auth token and I'm way down in my code at line 274,
so let's generate a token token and potentially an error are assigned the value of from the model's
package.
We're going to generate a and we have to pass it a user ID and we have the user, we pulled that from
the database.
So user ID, we need a time to life and we'll make Rs twenty four hours so twenty four times time hours.
And the last thing we need is the scope and I'm going to use the constant.
We declared in the models package models dot scop authentication and we'll check for an error.
If error is not equal to nil
then we'll just generate a bad request app, bad request and that just requires the response writer,
the reader and the error and will return.
OK, otherwise we want to send this token back.
So that means I'll only get a part of this payload now and a lot of cases, the only thing you want
to send back is the token.
But I actually want to send back the token amp'd whether or not there's been an error and the message.
So I'll just add to my payload struct something called token, which is a pointer to models dot token,
which is what we got back from our function.
And in JSON, I will call that authentication, underscore token and close my quotes and let's format
everything.
And I'll actually change the message from success just to make sure that I know I have all the necessary
information and to give really meaningful information back.
I'll call the format package as print F and I'll say token four percent s created and I'll substitute
user input or email for my percentages and I believe that will almost take care of it, except that
if I don't put the token in there, a little token
is equal to token and we're not actually sending back the token.
So that would be kind of pointless.
All right, let's give this a try.
So I'll open my terminal.
I'll make sure everything is stocked.
It is like start.
Everything started up let's go back to our Web browser, refresh this page to make sure it's current
or clear the JavaScript console and all, first of all, log in with invalid credentials so invalid
and invalid dot com with some random password, I should not be able to log in.
And I got invalid authentication credentials and I'll put in my one valid user admin, that example
dot com with the password, password, all lowercase and I should get a token back.
So here we are, object error, false messages token and we have an authentication token.
And if we look at the authentication token, you can see that has an expiry set.
Twenty four hours in the future.
And there is my token.
So this seems to work really well.
Now, clearly, there's some more things we need to do when we generate that token in our handler.
We actually need to save it to the database.
And there's a few other things we need to take care of as well.
So since we're saving it to the database, we'll have to have somewhere to put it, which means we have
to generate a migration or two and create some models and so forth.
But we'll get started on that in the next lecture.