All right, so that's pretty cool.
We now know how we can send a token.
But I mean, it's going to be a bit of a pain if we'll have to do that manually to all routes.
And just remember that it's not like you just need to set it up once, meaning every time you'll be
testing something, you'll have to change our token.
For example, if I log in with different user, token is going to be different.
So if you're doing this manually, I mean, it's going to be a little bit like a headless chicken where
you just need to run through all the routes and then the token value.
Now what is the better approach?
We take a look at the Redmi four register and log routes.
We want to go with pests over here, so add that to the Redmi test with a capital.
So in the test, we want to set up this code.
Where we have this PM, meaning poached maneuverable and in there we can access the response.
And essentially this is the response that we're sending back from the server and we want to go with
Jason.
And then we want to set up a global variable.
Remember when we set up the URL?
So this is similar.
The only difference is that we are doing this programmatically or dynamically.
How are you want to call them?
So we go with this command Global's set, then the name my Marcus, I'm going to go with token.
You can go access token.
You can go with JWT, whatever you want.
I'm Jason data.
We're looking for Dot token.
Why?
Well, because that's how the response is structured from our end.
So if the response is going to be structured differently, then of course you're looking for different
value.
This is very, very crucial.
So we're going to go to controllers and let me just show us something.
The reason why I go with Dot token is simply because when I'm sending this back, whether it's a registered
login, I have my object and in there I have the token.
That's a really, really important, and I'll discuss these ones once we have this setup in place.
So let me navigate back to the postman.
And first I'll just remove this authorization one so it can actually delete it if we want just so we
can clearly see that everything is working, let's save it.
Let's quickly tested out what we have over here and notice, and now I have this undefined as far as
the authorization header, and you're not in this case.
Well, actually going to go back to the fourth one and we don't need to.
I mean, it's pretty clear what's happening and I'm also not going to log what I'm interested is just
this author.
And at the moment, I can clearly see that it is undefined.
I won't because we're not passing it correct.
Now, how do we set this up programmatically?
Well, let's start here with registry user and then we'll do the same thing in logging in when we go
to the test and then we want to go with Konst and then JSON data.
So whatever response we're getting back again p.m. That's going to be the global variable.
And then we're looking for a response.
So that is going to be the response.
We're getting back from the server out and then we run a run of each someone, we invoke it.
And now let's right away set up that global variable and notice that even right away, give you a code
snippet.
And as a quick sign out if you're using environment.
The only difference is that you need to go with set an environment variable instead of global one.
Now, since my setup is global, that's why I went with the set global variable instead.
And then we go with Palm Global's set and what is going to be the name my cash.
I think I'm going to go with the token thing that makes more sense.
And as far as the value, well, now I want to access the response.
So Jason there and like I said, we go with dot and token, and once we have this one in place, let's
save it.
And what we're looking for in the update user is not the manual approach, but instead we'll look for
different app.
Now, before we continue, since this is a register and I don't want to remove the user, I mean, I
know that this code is correct, but since I don't want to bother with that whole song and dance, I'm
going to grab the code and I'll add it in, log in as well.
And when we'll be testing, we'll be testing with log in because I do agree that each and every time
me deleting the user, I mean, it can get annoying, pretty fast.
So let me navigate to the tests in the login copy and paste.
And now let's send it.
So we send it over here.
We set up that global variable and what you notice about in any of the routes where you want to use
it.
Instead of doing the manual approach, you go for authorization by default is going to be this one.
Inherit off when we want to go with bearer token and notice the value right away.
Now if you don't, of course, add this manually.
So we'll go here with two curlies and then the name of the variable, which in my case is token.
And also, yes, we can see that we're here once we have successful register or login event of this,
one will be added to our Global's nav means that each and every time we log in with a different user,
well, we'll also set this one automatically.
So now every time we use the update user, it will use that latest token.
Hopefully, that is clear.
And now, once I save it over here as well, I send it.
You'll notice that back on a server again, we have that token and now quickly add to the rest of the
route, so I don't need to worry about it for the remainder of the project.
So let me go to create job and I'll do the same thing.
Essentially, it will go with their token and with passion token.
Let's send right away and I'll test them at the very end, just so I can see whether I have some weird
typos or bugs or whatever.
But pretty much for all of them wounded.
Set up authorisation per token, let's send it, and we also have one for update jump as well as the
lead drum.
So all of these routes are going to be protected.
So let's go here and bear token and then let's send.
And once I go back to my server, you should see all of the accounts, and for all of them, you should
see the token.
And if that's the case, now, I can move on to the next step.
Where in the off the middleware, we'll check for those values and then, depending on those values,
will either pass along our request to the next controllers with already user on the request object or
will send back the.