- [Instructor] Okay so now the final piece
that we need to do is to do the JWT setup
inside of our index.
So let's go into the index file right here
and then what I'm going to do
just Below the bodyParser setup,
it doesn't matter where it is,
I'm going to add the JWT setup.
And this is going to be used to pass a secret word
and then take the headers
and make sure that we match when we get the request
and then pass it down to the controllers.
So let's go ahead and do JWT setup as the common.
And then we're going to do app.use.
And it takes a request, response, and then next.
So we can pass this next function.
And here we go.
So the first thing that we're going to need to do
is make sure that we have headers
which contains the token.
So first make sure that we have headers
and then make sure that we have in the headers
so request.headers.authorization.
And also make sure that in our headers,
and again, so all of this is going to become
very important when we make the request from Postman.
Want to make sure that we have authorization in the headers.
And also we want to make sure that we have
the JWT request or name inside of the headers.
So all of this we're checking in here.
And I'm going to do a word wrap
just to make sure that we're still going to be
on the same screen as I type more stuff.
Authorization.
And then we're going to use a little bit of RegEx and functions
with JavaScript
to check for
the keyword
that we're going to need as the secret word.
So let's go ahead and add JWT.
And then with JSON web token
we're going to validate all this.
So we get the data.
So if we have request headers
and then if we have request headers authorization
and if we have the word JWT in a request
then we can check and verify the signature.
It's a lot of complex code
just to make sure that what we get as a request
is authorized to hit the backend or the database
with new data or updated data and so on so forth.
So we're doing basically the same thing here.
And then we're checking the next thing in the array
and the next thing in the array should include
RESTFUL
APIs.
Remember, we added that keyword earlier.
And then we'll do the usual error
and then the decode.
So we're doing another function.
And if most of this doesn't make sense
I'll summarize once we're done typing all the code.
So if we have an error
then let's pass the request user as undefined.
And then the request user decode.
And then jump to the next function.
Else,
request user.
Again, we always need to make sure
that we're not passing data back
if we don't need to.
And that's part of what we're doing here.
So this is a lot of code
just to make sure that we have headers
and we have the proper JWT required elements
when we pass a request.
So want to make sure that we have, like I said before,
in the header the authorization
and that we have the word JWT.
And then we want to make sure
that we are passing in the header's RESTFULAPIs
when we send it to the database.
And finally we're using an if and else statement
to make sure that if we have all the elements of the request
or what we need here
we're not passing data back.
Okay so let's quickly validate that we have the import.
Oh, we don't, so we need to import JSON web token
if we want to be able to use it.
So let's make sure we do that.
Jsonwebtoken from
jsonwebtoken.
And let's do a quick check
if we have everything that we need.
There's no typos.
Everything seems to be fine.
Okay, perfect, so let's save this.
And now that we have our JWT setup,
we're ready to test this in the next video.