1 00:00:00,760 --> 00:00:04,780 As we were just discussing at the end of last video whenever we have this incoming requests that's going 2 00:00:04,780 --> 00:00:07,860 to cause our next JSA application to render out the app. 3 00:00:07,930 --> 00:00:12,850 We need to somehow take that cookie right there and included on any requests that are going to go back 4 00:00:12,850 --> 00:00:15,850 out to engine X to try to access other services. 5 00:00:15,850 --> 00:00:17,100 So we're going to focus on how to do that. 6 00:00:17,110 --> 00:00:22,790 In this video since we've had a long couple of videos here on some pretty crazy stuff I'm going to give 7 00:00:22,790 --> 00:00:25,500 you a pretty direct answer on exactly how to fix this. 8 00:00:25,580 --> 00:00:27,970 Whenever I get initial props gets called on a server. 9 00:00:28,070 --> 00:00:31,700 The first argument to the function is going to be an object that has a couple of different properties 10 00:00:31,700 --> 00:00:32,680 inside of it. 11 00:00:32,780 --> 00:00:36,250 One of the properties on that thing is the request object. 12 00:00:36,380 --> 00:00:39,110 I'm going to d structure out that request object. 13 00:00:39,320 --> 00:00:44,720 This request right here is the same kind of request object that we expect to receive inside of say an 14 00:00:44,720 --> 00:00:50,210 express J.S. application it's on this overall object as the REC property. 15 00:00:50,270 --> 00:00:53,030 So we will D structure off just wrecked by itself. 16 00:00:53,030 --> 00:00:57,380 Then take a look at all the headers on that incoming request because remember that cookie is passed 17 00:00:57,410 --> 00:00:58,260 as a header. 18 00:00:58,280 --> 00:01:06,280 We can do a console log right here of req dot headers and then going to save this all then go back over 19 00:01:06,270 --> 00:01:07,470 to my browser. 20 00:01:07,720 --> 00:01:13,600 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 21 00:01:13,600 --> 00:01:19,960 terminal and take a look at what just console logged so here is that headers object as all the different 22 00:01:19,960 --> 00:01:27,800 headers that were included in the initial request for my browser to Next j s and as you'd expect one 23 00:01:27,860 --> 00:01:32,830 the headers on there is the cookie so that cookie right there is the exact information that we want 24 00:01:32,830 --> 00:01:35,740 to pass along to ingress engine X.. 25 00:01:35,860 --> 00:01:41,470 So to make sure that eventually ends up with our service there are actually a couple of other very helpful 26 00:01:41,470 --> 00:01:42,740 headers on here as well. 27 00:01:42,820 --> 00:01:47,470 In particular if we take a look around you'll notice that the host header is actually present on this 28 00:01:47,470 --> 00:01:48,450 incoming request as well. 29 00:01:49,480 --> 00:01:54,400 So as a matter of fact rather than even having to specify the host right here one thing we might need 30 00:01:54,400 --> 00:01:59,440 to do is to just take all the headers on that incoming request and pass it through to the requests we're 31 00:01:59,440 --> 00:02:01,510 sending off to ingress engine X.. 32 00:02:01,780 --> 00:02:06,020 This would essentially cause this request right here to act as a proxy of sorts. 33 00:02:06,040 --> 00:02:10,300 They're gonna take a lot of attributes from that incoming request and just pass them directly along 34 00:02:10,510 --> 00:02:11,970 to our API call. 35 00:02:12,010 --> 00:02:14,580 So let's try that strategy out. 36 00:02:14,780 --> 00:02:20,570 I'm going to delete the headers object right there and replace it with req dot headers and then going 37 00:02:20,570 --> 00:02:22,100 to delete the console log at their 38 00:02:25,490 --> 00:02:25,750 OK. 39 00:02:25,990 --> 00:02:28,030 So let's save this. 40 00:02:28,120 --> 00:02:33,660 Now we do not have to worry about the request coming from the browser because remember the browser is 41 00:02:33,660 --> 00:02:37,050 going to handle appending on some headers to the thing for us automatically. 42 00:02:37,060 --> 00:02:41,130 So right now we really just care about the case in which the server make your request over to ingress 43 00:02:41,130 --> 00:02:43,890 engine X All right to test this out. 44 00:02:43,900 --> 00:02:44,950 I'll go back on my browser. 45 00:02:44,980 --> 00:02:50,620 I'm going to refresh the page and then I should see a console log on my current user right there in 46 00:02:50,630 --> 00:02:54,490 my console and back inside my terminal as well. 47 00:02:54,550 --> 00:02:55,750 Perfect. 48 00:02:55,750 --> 00:03:03,130 So now when we send our request from next J us off to ingress engine X a cookie and all the associated 49 00:03:03,130 --> 00:03:08,610 headers with this incoming request are gonna be passed along directly to the off service so the service 50 00:03:08,610 --> 00:03:13,350 is a matter of fact can barely even tell that this request is coming from our client. 51 00:03:13,390 --> 00:03:19,000 Instead the service as far as is is concerned it might as well have been getting the request directly 52 00:03:19,270 --> 00:03:24,750 from the browser itself the auto service would have a rather tough time telling the difference between 53 00:03:24,750 --> 00:03:26,620 the two all right. 54 00:03:26,630 --> 00:03:31,430 Well this looks like it is a good solution so that should pretty much be it as far as just getting our 55 00:03:31,430 --> 00:03:32,330 code working. 56 00:03:32,660 --> 00:03:38,120 But now what we're left with inside of get initial props is pretty darn nasty. 57 00:03:38,120 --> 00:03:41,990 We definitely do not want to have to write out this if statement every single time we want to make a 58 00:03:41,990 --> 00:03:45,100 request from a get initial prompts function. 59 00:03:45,170 --> 00:03:50,230 We don't really want to have to worry about the domain or passing through headers or anything like that. 60 00:03:50,750 --> 00:03:53,120 So even though this code works we're gonna take a pause right here. 61 00:03:53,130 --> 00:03:57,590 We can come back next video we're going to start to make a couple of improvements to it just to make 62 00:03:57,590 --> 00:03:59,540 sure that this is a lot easier to use.