Hey there, everyone they share.
And just one final video because I wanted to show you a little bit more ahead of that.
Now, we have worked on with everything we are able to log in, store the user in the database.
And in fact, if I go ahead and check with the database, I have logged in with multiple times with
the same email.
I'm not creating multiple entries.
This is all going in basic flow.
Nothing that we haven't seen so far in the code, but we are not yet protected on the home route.
The thing is that in the case of our application, which is our t shirt store, we were too much relied
on the token itself.
And that's why I told you that we can actually go ahead and create the cookie token, because if you
are going to allow this a serialization and serialization being happening, why are the passport?
Passport doesn't give you the tokens actually are not easily accessible.
You can access that but not that much easily.
They actually give you directly this request of the user.
So what we were doing in the past that how we were injecting the things and request dot user now the
middleware in case you are using passport as a total strategy for everything.
And by the way, just in case you missed that part, if I go on to the home, not home strategies,
if you look into the strategies, we have this passport local, which is a strategy absolutely designed
for what we have designed in the past, the username and the password or email the password.
So you want to use that.
You have to actually write your middleware a little bit different.
You cannot just copy paste the middleware that I have worked on.
You have to design it differently.
So in this case, we have noticed that inside the auth request that user is present if we get a successful
callback from the Google itself.
So we can use this as a simple middleware to check whether the user is logged in or not.
Just to give you a brief overview, I usually don't write my callbacks or these middleware up here,
but let's go ahead and add a middleware up here.
So we're going to go ahead and call simply const and we're going to call is logged in.
So if the user is logged in, this will be true and so, so on.
So we're going to go ahead and say request response and next, just like always, let's go ahead and
work on with that.
We're going to simply check if the user if there is a request or the user or not.
So we have seen that if the serialization works properly, token cookie session, everything is handled,
then the request user is present.
Otherwise not if the request of the user is not present.
That means rez dot redirect direct.
Is that correct?
Redirect?
Yeah.
Looks good.
Okay.
So if the user is not logged in, that means the request or the user is not present.
Then in that case, I would love to redirect you to slash or slash login.
And if he is logged in, then I'm not worried.
I'm going to go ahead and say, hey, just keep on, continue that.
So now I have this is logged in and I can protect any route.
Remember, this is exactly what we have been doing.
But in that case, we have to bring in the token, find the user.
There's a lot of us now.
Passport is taking care of all of that.
So all I have to do is inject this middleware wherever I want.
I want to inject it up here.
Let's go ahead and inject that, put a comma and that's it.
Now this route is protected.
If I'm logged in, then only I can access this one.
Let's go ahead and verify the theory that we have been presenting so far.
Let's go ahead and hit a reload.
Now, I'm not able to visit the home page.
If I go up it says, Hey, the request dot user doesn't exist, so you are not allowed to visit that.
If I log out, obviously I can.
Let's go ahead and hit on login where I'm already.
I'm going to go head into the Google quickly, go ahead and say log in and it gives me all of that data.
We are not redirecting it from here automatically.
We should be.
But this is kept here especially to show you what is happening now.
I can go back and I'm allowed to see the home page.
So this is a proof that yes, you are logged in and when I go ahead and do a log out, the log enable
cookie part of the session is now removed.
So that is important.
It still maintains a session, but I'm not able to visit the home page.
So this sums up the entirety of the things that how to use this one.
Now again, passport is a great way, but you have to think about your application from the very start
that are you going to use passport or not, because a bunch of things actually changes in the architecture
if you have application, your middleware and a bunch of other things.
And I've seen in some of the application, if you are planning a whole lot of social logins and beta
tokens and a whole bunch of other things, injecting the passport actually makes your life a little
bit easier.
But again, don't just blindly write the code.
Always try to understand what is happening, why is it happening and what's going to do when you're
going to change something.
So I hope you have enjoyed this one.
Let's go ahead and catch up and there's some video.