So let's give this a try.
Let's open our terminal and just run, make stir.
I should start at the front end and the back end.
And there it goes.
Now let's go over to our Web browser and see how many things we got right.
So I'll go to localhost four thousand and I will try logging in.
So when I look at my database right now.
I have one entry in the Tokens table that was created at twenty twenty one oh seven twenty three, sixteen
fifty three, so that's log, you only use valid credentials, for example, dot com with the password,
password and log in.
And now if we go back to our database and refresh this, the date has changed.
So now it's seven.
Twenty six.
Fourteen, forty three.
Perfect.
OK, so we have a token in the database.
Now let's open our JavaScript console.
Right here, and I'll make it a bit wider now, let's try going to the virtual terminal page and that
should call the check auth function and that should hit the back end and say, hey, here's my token.
Am I valid?
And it gave me a not logged in and took me back to the virtual terminal, so clearly we got something
wrong.
Let's see what that might be.
Let's go over to her console and see if we have anything longer unknown column t hash in the where clause.
That's why I put that message in my tokens.
Don't go the log.
We didn't actually call it hash in the database.
It's actually called token underscore hash.
So let's fix the token, underscore hash and we'll restart everything.
So may stop
start.
It's running again, so let's go back to our Web browser, go to the homepage, we're still logged in.
Let's clear the JavaScript console and now let's try going to the virtual terminal page.
And it says, log in, perfect.
Now let's go back to the home page and log out and try typing Virtual Dush terminal right in the in
the browser's address bar.
And that should take us back to the login screen.
And it does.
Now, just as a little note here or an aside, if you want to use this method, this form of authentication,
you can do it and there's nothing wrong with it whatsoever.
Let's go back to our I.T. and let's look at base layout going HTML.
What you would have to do if you really wanted to make this your one form of authenticating people is
you wouldn't want checkoff to be where it is right now.
At the bottom of this this particular file, you would move it up into the header, say, right up here
just after the title.
OK, you would put it here.
So it's in the head and on your terminal page here.
Instead of calling check off at the bottom of the page, you would again put it right up here in a new
block that you would define that appears after where your function would be right here.
So I would put a script tag like this script, and I'm going to get rid of this script and I would put
the check off function.
Then after that, I would put something like this block in head or whatever you want to call it and
hand it data.
OK.
And then end it so in terminal page, when that's rendered, we would already have the check auth function
right here in the hat and a terminal page, I would put my check up here.
I would define in head and put my call to check off.
And the reason being the way the browsers work right now, at least, is that as a page is being rendered,
as soon as it finds that script tag in the head, everything comes to a screeching halt.
It executes that JavaScript and then it moves on.
And that's the way browsers work right now, at least.
So if we put our check up in the head, the user is not going to see that page until that JavaScript
is executed.
And if that fails, authentication, if the token we send either doesn't exist or is expired or is invalid,
then they get redirected to the login page before they see anything else.
So that is one approach.
Now, I'm not going to do that because in the coming lectures, I'm going to implement another form
of of checking for authentication that's handled server side with sessions.
So we have this working right now.
We have a means of sending a token to the back end to seeing if it's valid and that works really well.
But before we go too much further, we should probably write some middleware that allows us to authenticate
users in a much more meaningful fashion.
And the logic is going to be more or less the same, but it's going to make it much more usable for
us if we extract everything to middleware and do our authentication based upon the route that is being
accessed.
So we'll get started on that in the next lecture.