So as I suggested last time, we need to improve the way we're working with sessions right now, we're
using a very simple session store.
It's just cookies and there's nothing wrong with those.
And they work fine.
But in development particularly, it gets annoying pretty quickly.
And I'll show you why, because it's not apparent to the user, to me as I'm working, that I've actually
not logged out or logged out or things aren't exactly what I think they are.
So I have my application running log in admin at example.
Dot com password.
Password.
Great.
So it appears that I'm logged in, but if I go back to my application, my idea and I open my terminal
and I stop this application, I stop my front end sessions are gone and I'll start the application again.
Start.
And there it is, it's running when I go back to my Web browser and I click on, say, the products
page by one widget, I still see logout up here.
And that's because the way I'm determining whether or not that menu item shows log out instead of log
in is based upon the presence of a token stored in local storage.
But in fact, my front end off is entirely gone.
So if I go to a virtual terminal, it takes me to the login screen and it still tells me that I'm logged
in because it says log out up here.
So that gets annoying in a hurry.
So we're going to fix that and we're going to do that by using this package, which is an add on to
the session manager we're using, and it's called Alex Edwards, slash X, slash my my MySQL store or
my Seiple store.
OK, so installing this is really simple.
So the first thing I'll do is look at the documentation and right here it says setup.
You should have a working MySQL database containing a sessions table and it shows me how to create it.
So I'll copy this sequel and go back to my idea.
And in my terminal I'll clear the screen and I'll type.
So to generate and this time I'm going to generate a sequel, not Fears and I'll call it Create Sessions
Table, and that creates two migrations up and down.
So let's go to our migration's folder and I'll find the up migration, which is right here, and just
paste that sequel in there.
And the down migration is just to delete this session's table.
So here in the down migration, I just type drop table sessions and save it.
Now if I run this migration, so to migrate now I have a sessions table.
So if I go look at my database
and reload this, there is my first sessions table right there.
OK, very simple.
Three columns.
So now how do we use this?
Let's go back and look at the documentation.
Obviously we need to import this.
We need to go get this.
So I'll, I'll copy this.
GitHub, Dotcom's, Alex Edwards, so on and so forth, ending with MySQL store and go back to my Edem,
clear my screen in my terminal and just type go get and then paste in that you URL.
And that goes and gets it for me.
So that means I need to go to my mango and where I'm creating the sessions right here.
I just need to make a little change after line eighty six in my coat.
So let's go back and see what that changes.
The documentation says basically do this session manager store equals my ask.
You will store new and hand at your database.
Well can we do that.
We have the database in the right spot.
In fact, we do.
We have it right here called Köln.
So all I have to do and this is very simple configuration, but it's sufficient for our purposes right
now is say session that store equals MySQL store and hopefully you will find it.
It did dot new and had my database.
Now when I stopped my application, like stop and make start.
OK, and I will go back and look at my Web browser, go to the correct tab, refresh everything, OK,
so it's running.
Now let's look at the database table sessions and see what's in there.
So I'll refresh this.
There's nothing in there.
Now let's log in admin app example, dot com with the password, password and log in.
OK, we logged in and I can get to the virtual terminal, great, so let's go back to the home page.
Let's look at the database table and see what's in there now.
And now we have one entry.
There's my token.
OK, so let me go back to the.
Stop the application.
Make stop, OK, and now make a start again.
Last time we did this, we lost our session, so it's running again.
So I'm still logged in.
And if I go to say the products page still says I'm logged in, but can I go to a virtual terminal where
we're actually checking for the presence of that seminal variable user ID?
And I can and that's going to make things ever so much easier as we progress through the remainder of
this course.
All right.
Let's move on.