We've now got all four of our different primary root handlers but together we're not going to go back
into our current user root handler.
We're gonna take a look at some logic inside of here and start to extract it into two separate middle.
Let me tell you exactly why we're going to do this at some point time.
We're going to have some other services running inside of our project.
Right now we only have all service but at some point we will also have users tickets orders whatever
else and all these other services we put together.
We're going to inevitably at some point in time have some incoming requests from a user who may or may
not be logged in to things that we're almost definitely going to have to do at some point time is check
to see whether or not that user is logged in and if they are not we are probably going to want to prevent
access to some given root handler.
The other thing that we are just about definitely going to have to do is take the information out of
the Jason web token payload and set it on record current user.
There's nothing special about wrecker user.
This is just a very common property to use.
Anytime that you want to present some information about the current user and get access to it inside
of our root handler.
So again these are two middle where's that we're just about definitely going to want to have at some
point time.
So we might as well create them right now as long as we are handling all this authentication stuff and
we've got this idea of Jason Webb tokens fresh in our minds so in this video and the next one or two
let's create these two middleware we're going to first create the middleware that's just going to extract
information out of that payload and set it on rect current user so that it can be can be consumed by
further handlers or Middleware is down our chain.
So back inside my editor I'm going to find that middleware directory.
Once again we're gonna create a new file inside their called current user dot T.S. and then inside of
here we're going to add in one or two imports at the top and then set up the middleware function it's
going to import requests response and the next function types from Express I'm also going to import
JWT from Jason web token because of course we are going to want to decode our Jason what token at some
point and we'll go ahead and export a new function called current user
this thing is going to take some number of arguments because this is not an error handling middleware
it's going to take three arguments of req rez in next.
So we will put those arguments in and annotate each them as well.
So req is of type request.
Rez is our type response.
And next is of type next function like so
so inside of here what we want to do is pretty straightforward.
We are only trying to figure out whether or not the user is logged in and if they are we want to extract
information out of that payload and set it on rect current user.
And again there's nothing special that that current user property it's just a very common property to
set.
To describe who the current user is inside an express application if the user is not logged in.
We are not going to throw in air or anything like that that is going to be the job of the other middleware
we are going to put together.
So the only job this one is to say hey if you're logged in are going to extract information that payload
and set it on this given property all right.
So we will first put together a quick check to decide whether or not the user has a Jason web token.
And this is going to end up being very similar to the logic we have put together back over here inside
of our current user handler we're going to say if there is no session object.
Remember that's why we've got that little exclamation or if there is no JWT property.
So remember that little question mark right there is going to get typescript to kind of expand its entire
if statement into two separate checks.
We're going to first make sure that session is defined and if it is then we'll try to access the JWT
property so if we do not have a session object or if JWT is not defined we want to return and move on
to the next middleware inside of our chain so we will put inside a return next.
Like so
then if we get past that point we're going to want to decode the Jason web token once again very similar
to what we did back inside of current user.
If there's anything wrong with the decoding process we're going to probably want to make sure that we
capture that error and send back or continue on saying oh well the user must not be logged in so I'm
going to add in a try patch statement inside of the try.
We'll try to get the payload out of that Jason web token to remember how we do that we call data T dot
verify we'll pass in the Jason web token which is rec session JWT and then the second argument is going
to be our actual token which is process envy data beauty key once again we've gone over this three times
typescript thinks that this variable might be undefined but we already did a check for that back inside
our indexed TSR file so we're gonna put the exclamation on there to just say hey typescript don't sweat
it this thing is defined.
Now if anything goes wrong with trying to extract the information then we will add end up inside of
our catch statement so we could put in a next right here to just try to continue on but it turns out
that no matter what.
Whether or not this thing throws an error we really want to call next to continue on to our next middleware.
So we don't necessarily have to put that inside the catch statement.
Instead we can just added in right after like so as again whether or not we decode that token successfully
we always want to continue on to the next middleware now that we've got that payload we need to actually
set it on our request object.
Again we are setting on the request object.
So for their middleware is where other root handlers down this chain can figure out automatically who
the current user is.
So what we would like to do is wreck dot current user is payload.
That would be ideal.
But as soon as we do that we didn't air from typescript script to saying hey there's no current user
property on a request object.
So it turns out that this is gonna be just a little bit harder to do than we might have expected.
You and I want to add in a new property to an existing object type that is already defined inside of
you type definition file.
As far as Tikrit script is concerned we can not just arbitrarily add in new properties.
So we need to figure out some way to tell typescript Hey don't sweat it.
It is OK.
There actually is a current user property for a request object.
We're gonna have to figure out how to work with typescript a little bit more closely and tell it that
it is okay that we are adding in this additional property.
Let's take a pause right here and then tackle that in just a moment.