In this video, we are going to create a murderer.
And that murderer is going to check if indeed a user is logged in.
If a user is logged in, we're going to have a property on the session as user of.
So let's see how we are going to implement that.
Inside the middleware, we are going to create one file and this one I'm going to name it as protected.
Dot JS.
Or you can make it as is locked in.
All right, so let's see how we're going to create that one.
So I'm going to create a function call and I'll call this one as protected.
And is equal to every function, and I'm going to pass in the req response and then the next.
So we are going to check if a user is log in.
So here, check if the user is logged in and if a user is log in, we have access of the user on the
record session.
So here we go if we have a property on the rec dot session.
Dart user of.
Then it means that the user is locked in and I'm going to call next.
Ere you are going to throw an error by saying that not allowed or permission denied.
So let's require the app error.
All right.
It wouldn't work.
So let me make sure that I received the file again.
And here I'm going to make use of up error.
Now I have the auto import, so I'm going to call next and I'll pass in the error.
And the message I want to send is not authorized.
Log in again.
All right.
So now I'm going to pass this one as a mirror to our route.
So let me export this one as protected.
So any route I want to check if a user is logged in, I'm going to pass this one as a middle word.
So let me get back to the user route, which is this one.
For the meantime, let's protect the profile, so let's require it here as context protected.
Forced lash made worse and then protected.
So let's protect the profile.
So here I'm going to pass this as a second argument to this as protected.
So now let's make a request to the profile endpoint and.
Here is a profile end point, which is this one.
So I'm going to send the request and go back.
The response has not allowed me not authorize.
Log in again.
So let me go ahead and log in and let's see the effect.
All right.
So let me get back here and log in.
Fine.
I have log in.
I have the user on the session and let me click on this one sent.
And now you can see that you go back the response from the controller of the profile.
For the meantime, we are sending the static text here.
But let me show you one thing here.
As soon as I restart the server, let me clear the terminal and then restart the server and let me hit
the same endpoint for the profile.
And you can see we go back not authorized, meaning that I'm not logged in even though I have logged
in.
The problem is that as soon as our server gets restarted, it loses the session, meaning that the session
that has been generated, it is saving into the memory of our application.
That's why it got lost upon every restarting of the server.
So how are we going to process that?
So to avoid this, we are going to save the session inside our database than saving into the memory
of our application.
So let's see how we are going to achieve that one in the next video.