So how did you make out with the challenge?
Hopefully you noticed there were a few steps necessary to make this happen.
Well, let me show you how I'm going to do it.
I'm going to, first of all, look at my database.
So let's look at our database.
And you will notice that nowhere in the Tokens table.
Let me look at the structure.
Nowhere in the tokens table do we actually have the expiry.
So we have no means of querying the database to see when this tokens might expire.
So first of all, let's create some migrations.
So I open my terminal and I will say so to generate FIDs and expiry two tokens and that will create
my up and down migrations.
So let's open those right under migration's and I'll open the up migration first and then I'll put in
here is a simple line add column and I want to add it to the tokens table and I want it to be called
expiry and I want it to be a timestamp and I'll give it a default.
I'll give it no default at all.
OK, so which means we're going to have to delete our entry from the Tobins table right now to avoid
a null error, but that's no problem.
And then we'll go to our down migration and that's simply drop column
from the tokens table and we want to drop expiry.
OK, so let's run this migration and make sure it works.
So the migration.
And there it is, it applied the migration and I always get this error, Wawn ignoring file schema don't.
Just ignore that it serves no useful purpose.
So now we should have a column in our database.
Let's make sure
to refresh this.
And there is expiry.
OK, so I have that and I have a place to put it.
And while I'm at it, let me go to my tokens table and delete that one entry that's in there.
Delete selected.
Perfect.
OK, so now let's go close these two migration's and Heider Migration's folder and let's go right to
the roots for our API so API roots, so authenticate API slash, API slash authenticate.
That's where we do our insertion of an entry into the database.
So let's make sure we have the necessary logic in there.
So I'll go to create Tolkan or create off token so we get our user input.
We read the JSON, we try to get the user from the database by email.
We make sure their password matches and down here we generate a token.
And as you can see, the second argument to this function generate Tolkan is our expiry.
We expire in twenty four hours.
So I know at this point that my token has the necessary expiry because we're populating it based upon
this.
OK, so we then save it to the database.
Here's what we have to make our first actual change other than the migration we need to insert into
tokens, user ID name, email, Tolkan Hash and then the expiry.
OK, which means down here after the hash I just add t dot expiry.
There it is.
OK, so that will inserted into the database.
And of course the other change we need to make is when we're saying get user for Tolkan down here.
And this is the bit we were working on in the most recent lectures.
We just have to modify this to say and t dot expiry is greater than questionmark.
And then we come down here.
And we add time now, and that should be all we have to do.
So let's try this was Clearstream and make a and we see if we got it.
All right.
And we'll go back to our idea and go to the homepage and I'll log in and I'm going to log in as admin
at example, dot com with the password, password and log in.
And we have an error column count doesn't match value count at row one?
So let's see where that mistake is.
One, two, three, four, five, six, one, two, three, four, five, six, seven.
There it is right there.
OK, let's add that questionmark.
I do that all the time.
Restart things, make stop, make start.
Let's try this again back into our Web browser, refresh this page, OK, and log in as an example,
dot com password and log in.
OK, so we're logged in.
We should be able to see the virtual terminal page and we can.
Now, let's go back to the home page and let's go to the database and change the expiry for the record,
that was just inserted.
So I refresh this and I'll make the.
Expiry, I'll just put it one year ago, and save that and come back here and try going to the virtual
terminal page and it redirects us so expiry works the way that it should.
OK, that wasn't terribly difficult.
Now it's time to go work on some middleware so we can secure things like this, even though I'm logged
in.
If I go to admin at example, dot com and password.
And again, this is great and I can now see the virtual terminal page.
The problem is that the route behind this page that actually calls the back end using our JavaScript
fetch statement, that's not protected.
And we need to make sure that it is protected.
And we'll get started on that by writing some middleware and changing some of the logic on the front
end in the next lecture.