As we were just discussing at the end of last video whenever we have this incoming requests that's going
to cause our next JSA application to render out the app.
We need to somehow take that cookie right there and included on any requests that are going to go back
out to engine X to try to access other services.
So we're going to focus on how to do that.
In this video since we've had a long couple of videos here on some pretty crazy stuff I'm going to give
you a pretty direct answer on exactly how to fix this.
Whenever I get initial props gets called on a server.
The first argument to the function is going to be an object that has a couple of different properties
inside of it.
One of the properties on that thing is the request object.
I'm going to d structure out that request object.
This request right here is the same kind of request object that we expect to receive inside of say an
express J.S. application it's on this overall object as the REC property.
So we will D structure off just wrecked by itself.
Then take a look at all the headers on that incoming request because remember that cookie is passed
as a header.
We can do a console log right here of req dot headers and then going to save this all then go back over
to my browser.
I'm gonna do a full refresh of this page but here's a full refresh and then I'll go back over to my
terminal and take a look at what just console logged so here is that headers object as all the different
headers that were included in the initial request for my browser to Next j s and as you'd expect one
the headers on there is the cookie so that cookie right there is the exact information that we want
to pass along to ingress engine X..
So to make sure that eventually ends up with our service there are actually a couple of other very helpful
headers on here as well.
In particular if we take a look around you'll notice that the host header is actually present on this
incoming request as well.
So as a matter of fact rather than even having to specify the host right here one thing we might need
to do is to just take all the headers on that incoming request and pass it through to the requests we're
sending off to ingress engine X..
This would essentially cause this request right here to act as a proxy of sorts.
They're gonna take a lot of attributes from that incoming request and just pass them directly along
to our API call.
So let's try that strategy out.
I'm going to delete the headers object right there and replace it with req dot headers and then going
to delete the console log at their
OK.
So let's save this.
Now we do not have to worry about the request coming from the browser because remember the browser is
going to handle appending on some headers to the thing for us automatically.
So right now we really just care about the case in which the server make your request over to ingress
engine X All right to test this out.
I'll go back on my browser.
I'm going to refresh the page and then I should see a console log on my current user right there in
my console and back inside my terminal as well.
Perfect.
So now when we send our request from next J us off to ingress engine X a cookie and all the associated
headers with this incoming request are gonna be passed along directly to the off service so the service
is a matter of fact can barely even tell that this request is coming from our client.
Instead the service as far as is is concerned it might as well have been getting the request directly
from the browser itself the auto service would have a rather tough time telling the difference between
the two all right.
Well this looks like it is a good solution so that should pretty much be it as far as just getting our
code working.
But now what we're left with inside of get initial props is pretty darn nasty.
We definitely do not want to have to write out this if statement every single time we want to make a
request from a get initial prompts function.
We don't really want to have to worry about the domain or passing through headers or anything like that.
So even though this code works we're gonna take a pause right here.
We can come back next video we're going to start to make a couple of improvements to it just to make
sure that this is a lot easier to use.