- [Instructor] After all this talking,
now let's get ready to add redis sessions
to our application,
and for that already opened our app.js file,
because this is where all the session management
is configured.
Then first have to install a model for that.
And this module is called connect redis.
So now type NPM install connect dash redis.
After that I start the application again within NPM run Def,
and now right after the session was required
I add const redis store.
So this is the redis session store
equals require connect dash redis.
And now very important is argument now.
So in parentheses, I add the session object.
So this is what the register needs to parametrize itself.
And then I scroll down to the middleware in line 19
where the session is configured,
and there I can now simply add, store,
because this session middleware is really very pluggable.
There can now simply pass in the redis store,
and now that's very neat.
I can just add an argument,
a configuration option client.
So this is in curly brackets.
And there I can now use the client object
that we created before,
because this client object is compatible
with this connect redis store.
So now can go ahead and just write,
config.redis.client.
So I'm passing in the client that we configured already
and this should be it pretty much.
So when I hit on save,
we see the application starts properly.
Now to see if this works, let's open the browser.
And there, I open the user management
and let's create a new user.
So I create jane@acme.com,
I give a password and I hit save.
And as I told you before,
this user notifications are using sessions.
So there should not be a session also stored in redis.
And to see the session,
let's look into redis insight,
and there I have to refresh the page.
And as you see, we have now this object here.
This says object,
and maybe first you see that the sessions are named
S-E-S-S then the colon,
and the long string after that is the session ID,
and it's used by redis also as hash key
to access the session information.
So this is the way how redis can now very efficiently
access this session information.
Now let's go back into our shopping application
and let's click on this email address,
because as you see this will call this impersonate route
I talked before,
and this impersonate route
should store a user ID in the session.
So let's try this out.
I click on that,
and let's look back into the redis insight.
Let's refresh,
and you see now we also store this user ID
from the impersonal route in the session.
So that works already great.
Now let's see what happens
when a new user comes in for that.
I will just use the incognito mode of the browser,
and access to page.
And now I click on this john@acme.com,
so I impersonate John.
And in redis inside I reload the page again.
And there you'll see that redis created a new element
for this new user.
What's also interesting here compared to other databases
is that now each session we'll create a new entry here.
So this is completely normal in redis.
Maxcoin values is one key and it contains a set,
but it's also completely valid
to have one namespace per session as we have it here.
With the session management working,
we can now start implementing our shopping basket
on top of it.