Hey, did everyone they share?
The one last thing that is remaining in understanding the passport is we have worked through we have
understood the call back, the strategy, everything.
Now there is one thing that is remaining.
Now, whenever we use the JSON token, we actually send this JSON token in the cookies itself so that
whenever you make a request, I can actually look into the cookie, extract the ID from the token and
can verify that you are the user what you are saying.
So for this also in the first place, I have to actually send this.
I'd have to wrap it in the token and send it to request user.
So there are two steps of processing that I have to kind of wrap it around, throw it into the user,
then every request I have to bring it up and that and see that what you are saying, this is exactly
what we need to do here in the passport.
In case you are not understanding what I'm saying.
This is exactly we have spent the entirety of couple of even sections and probably more than ten videos
and understanding this exact thing.
This exact thing happens in the passport, but it is so vague and so abstract that I didn't bring it
up first in the course and I'm bringing it in the very last of the course.
Now, in case you notice, you can use a whole lot of configuration notice here.
This is what we are missing up here.
So if I scroll a little bit up here and you can see there is an option of passport sterilizer and passport
DC user, this is exactly what is happening.
It takes that user ID wraps it up in the cookie and the token and everything and throws back whenever
it is required and it also serialized it.
So whenever a request comes in, it just takes it first.
Remember, it's a middle way, it happens both way.
So it always go ahead and come back and use this one.
But before we go ahead and use this one, you can actually use whatever you like.
You can use sessions, you can use cookie, you can use cookie session, whatever you like.
The only thing that you have to do is not use this passport in initialization we have already done,
but we have to also say that about use and passport session.
So this is something we are going to go ahead and use that.
Let me copy that first and move it up onto the index or JS.
So just where we actually go ahead and went up with the initialization of this passport, we're going
to go ahead and say, hey, just go ahead and manage a sessions for me as well.
Now in this of the application, we are not using cookies or anything, we are using cookies session
instead of that.
So we're going to go ahead and take advantage of this cookie session as well.
So we're going to go ahead and scroll a little bit.
And here you can see there is a middleware for app use and then we have express session and just below
should be somewhere the cookie session here.
Let me go ahead and quickly see that och looked at a little bit there and found it on the passwords.
Yes.
But actually rather was able to find it up here.
Now the whole idea behind using not just the cookie and cookie session because what passport actually
supports are the sessions.
So they actually have something in the session.
The one the library, which is famous among all the developers is cookie session.
What it does, it actually stores everything in the format of cookies, but actually also supports the
session.
So this is kind of a hybrid in between.
So we have already installed this cookie session and we have a nice example up here.
We have already done the installation part and if you scroll it a little bit and this is all you have
to do, so bring in the cookie session.
So let's go ahead and bring it up first up here with the first line and let's go up here into the index
or just at the very top.
Let's go ahead and bring in and we're going to convert that into const because we are not changing it.
And further down the road, all I have to do is add it as a middleware, just like we added the middleware
for the cookies and we forgot about it.
So just like that, we have to use that.
So let's go ahead and copy this and paste it up here.
So we're going to go ahead and say that, hey, we're going to use passport session, but let's use
it at the top up here.
There we go.
And the name the name is going to be not used in this case.
You can actually provide a dedicated name in case you wish to.
We're going to go ahead and provide another property, which is Max H.
So it's a cookie.
It needs to have age.
We have been using so far three days, but feel free to change.
Go ahead and change this one.
So this needs to multiply it by 24.
This needs to be multiplied by 60 further down the road, 60 and further down the road.
Thousand.
So this is all what we got.
Let's put a comma now.
Keys.
We won't be providing multiple keys.
We'll be using just one keys.
This is exactly like the JSON token.
So just like the JSON has the key, this also needs to be a secret key.
So this is ae token key.
And again, of course, you know that this needs to go into the Dot D and V, save that, and that's
all you have to do.
Now, one more thing.
It's not we are all done.
We have talked about this, how the sessions are going to be managed and everything.
But remember, we are still missing the key thing.
We have passed on everything and all of that, but right now they are still not able to access that.
How we're going to actually do that for this, we have to see the lies and deceit lies, whatever the
value we are passing on.
So let's go ahead and scroll that.
And this is exactly the piece of code that we are going to need.
Copy that and bring it up here.
So inside the Google strategy, just where we are using this passport dot use, these are also middleware.
So.
As soon as you are bringing this file, the sterilizer and Digitizer will also go into extra jazz.
We have a cookie session there.
We have the passport session being injected, so too many materials are injected and they'll manage
the things on their own.
Now notice here in the done.
Now this time you are passing users ID this is absolutely fine.
You don't need to say user or underscore ID now also it is saying that at the time of DC user, when
it will bring the things back, it will automatically unwrap the things we don't need to do this time.
JSON verify and JSON sign.
We don't need to do that.
It will automatically manage the things for me.
Now when you are saying this user ID at the time of bringing and decentralizing it will automatically
get this ID, you'll have this function and if you are going to find this, then we are going to have
this done and we are going to have users.
So this seems right out of the box working fine for us.
Let's go ahead and save this one.
And hopefully this time things are going to be much, much better as what we have seen as we are at
the end of the section.
So let's go ahead and work on with this one.
So let's go ahead error and let's see that how much progress we have made in this one.
So we are going up here.
Let's go ahead and move on to log in.
Let's go Google and this time again, I want to log in and this time I'm able to successfully see the
underscore ID, I'm able to see name and my email as well.
So this proves that definitely there is something which is properly serializing and properly decentralizing,
even managing the application.
And also don't forget this part that we have this request of user.
So just like we have been previously using all these middleware for request dot user, now we have access
of that.
Now one more thing very, very interesting thing I would like to show you is right click and inspect
and we are going to go into the into the application.
Let's go up here, the notice here inside the cookies, we have this localhost cookies, which is the
express sessions up here.
So this is exactly the cookie that is being injected.
Now, let me go ahead and code out a simple kind of a log out for you and show you what happens in that
case.
So even if I go back on to the home page, obviously not like that.
Obviously this cookies or this session will actually remain as it is up here.
So there we go.
These are still there.
Now, let me code a little bit of the code so that we can have a log out route as well.
If I go into the log in, we have this log out, which is all slash log out.
So let's go ahead and quickly get of that.
And this one, there we go.
We can quickly code that out.
Shouldn't be a big deal.
Log out.
And how do we log out?
That is the big question in front of us.
Now, in order to log out anything, there is nothing much.
All you have to do is request dot log out.
Again, thanks to the passport, courtesy of the password that they are giving it.
Now, we don't want to hang it around up here, so we're going to go ahead and say, Hey, Rez Dot Redirect
and we're going to go ahead and redirect on to slash auth slash login.
So in the case of redirecting, we cannot actually rely on the middleware.
We have to pass on the proper path where we want user to redirect.
Let's go ahead and save this one and I hope you can see this up here that even if I refresh or anything,
my tokens and everything are still there.
But this actually now my logout should be working properly if I log it out.
What I want to show you is what this is comprised of.
So this entirety is really a big kind of a chunk up here that we have got up here.
Now, if I go ahead and hit the log out the notice, it has changed a little bit there.
A lot of time people say, hey, we still have a session token and everything.
So why did it it cleaned it out.
No, it's not cleaned out because these are not log log in sessions now.
It still maintains a session in your application, but these are not log enabled sessions.
So just keep that in mind in case you want to go ahead.
You can clean the cookies as well.
We have seen that in the past, but these are not something that you should be too much worried about.
But again, clearing them is always a good habit.
Always.
Okay, so this is all good.
But I would like to show you just one more last piece of the thing that is not too much important,
but I would love to still show you.
Let's go ahead and catch up in the next video.