So we've managed to generate a token and send it to the front end and everything seems to be working
really well, but of course, once we send that token at the moment, we forget about it.
We have no means of looking at a token sent to us from the front end validating that it's one we issued
and validating that it hasn't expired.
And we'll be doing all that before too long.
So obviously, we need to save it somewhere.
And obviously we're going to do that right here in Handler's Dash APG go in the create auth token handler
before we send the response.
But after we've generated the token we'll say save to database.
We're going to save the token to the database.
And of course that means we need somewhere to put it.
So I'll open my terminal and I will generate a migration.
So to generate fears and I'll call this create tokens table and then we'll create two tokens for us
or two migration's for us.
So I'll open my migration's folder and as I usually do, because I don't want to have any typographical
errors, I'll grab an up migration, say this one, for creating the customer's table copy and paste
it into my newly created up migration and make sure you get the up migration and I'll just modify this.
So I'm going to create a table called tokens and the first column will be an ID primary is true.
Find the second one, I'll make the user I.D. and that will be the user's ID, which is of course an
integer and it is unsigned.
So let's make it unsigned again.
Unsigned, true.
The next one I probably want to save at least their last name, so I'll just call it name string and
that's fine.
And I want to save the user's email as well.
And then I want to store the hash.
So we'll duplicate this line and I'll leave it.
I'll call it hash
and it is a string right now, but it actually needs to be storing a slice of bytes.
That's what our hashes.
That's not a problem.
What I do, first of all, is modify these two SQL statements to set the default.
Now for the timestamps.
So the table is tokens and the table is tokens.
After I generate the table, these three are these two SQL statements, we'll get executed and I'll
just add one here and modify it so it's not going to be setting a default.
Instead, I'll just alter table tokens and I'll say modify hash I think hashes a reserved word.
So I'll call it a token token hash and I'll set the verb binary with the length of two hundred and fifty
five.
And that means token hash here gets put up here, token hash again token hash that was stored down migration
which is simple drop table tokens.
And if everything went well I should be able to clear the screen here and say sota migrate and I'll,
let's go look at the database and make sure we have everything in the right format.
So this is our database.
Let's refresh this and look at tokens.
So we have ID for our very, very binary.
Looks good.
OK, so we'll close that and now we're ready to actually write some database functionality because we
want to insert the tokens.
So I'll close my two migrations and I'll go over to I'll go to tokens dot because that's in the models
package so I can create a new function here.
And because I'm in the topin in the models package, I can use the receiver m pointer to DB model.
I'll call this insert token and it will take two arguments.
The first one will be the token.
I'll just call that T and it's a pointer to token again token.
And the second one is the user we're going to get the information from and I'll just call that you for
user at.
Potentially this will return an error and I'll get rid of that extra parentheses.
OK, now as I often do, I'm going to open the models Dargo file and copy this context stuff because
I invariably typed that incorrectly and I'll just paste that in here.
And that means I need to import context up here context and I should make the error go away or one of
errors.
But right now, let's create our statement.
So we'll say statement is assign the value of insert into tokens and we're going to be putting in the
user already their name and email hash, which I called hash.
What do they call the token hash?
Then I make sure hash, token or token hash.
I called it token hash, so I'll copy that.
Token ash and then just created and updated and we're going to be inserting the values of one, two,
one, two, three, four, five, six.
So one, two, three, four, five, six.
One, two, three, four, five, six.
Perfect.
So that's our statement.
Now, we'll just execute our statement so we don't care about the result, but we do care about the
error or assign the value of from the receiver m db exact context and we're going to find out the context
and we're going to hand it our statement and then all of the values for our placeholders, which are
for the first one we're going to put in the user user last name, the user email.
Then we're going to put in the hash from the token hash.
And I can just put time up now and time down now.
OK, we'll check for an error.
If fair is not equal to nil, return the error, otherwise return nil.
Now, if that all was written properly, I can go back to Handler's apart and actually just save information
to the database.
So we're checking for an error.
Error is equal from app to insert token and hand it our token, which I call token and our user which
are called user.
And we'll just copy this error checking, paste this in here, open our terminal, clear the screen,
make a stop to make sure nothing is running Mixter.
Now, it should be able to go back to my Web browser, reload this page and put in my values that mean
at example, dot com password log in, we got no error.
And we have Tolkan for admin at example, dot com created.
Let's go look at our database and see if it's actually there.
So when the tokens table, there it is.
So we have our hash, we have our email.
We have everything we seem to need.
Perfect.
So now we need to do something with that token on the front end and we need to set up some authentication
middleware on the backend for protected routes and we'll get started on that in the next election.