1 00:00:01,090 --> 00:00:05,320 In this video we're going to take a look at a very easy way to capture the cookie that gets sent back 2 00:00:05,320 --> 00:00:10,350 to us during this initial request and include that cookie in the follow up request right after it. 3 00:00:10,360 --> 00:00:13,390 We're going to first take a look at this very easy way of handling this. 4 00:00:13,480 --> 00:00:17,830 But then after that we're going to start to do a little bit larger refactor to make it really easy to 5 00:00:17,830 --> 00:00:22,600 make authenticated requests in the future because again just about every other service we're going to 6 00:00:22,600 --> 00:00:27,180 put together and write test for we're going to have to be authenticated to make requests to it. 7 00:00:27,190 --> 00:00:32,290 So we want make sure that in a testing environment it is really really easy to get authenticated. 8 00:00:32,290 --> 00:00:32,530 OK. 9 00:00:32,530 --> 00:00:35,020 So here's the very easy solution. 10 00:00:35,020 --> 00:00:39,460 We're going to first take a look at the initial request right here which should be giving us back a 11 00:00:39,460 --> 00:00:40,740 cookie. 12 00:00:40,780 --> 00:00:43,360 I'm going to capture the response so we get sent. 13 00:00:43,390 --> 00:00:49,660 So I will call this something like off response maybe a better name for it would be like sign up response 14 00:00:49,660 --> 00:00:51,070 or something like that. 15 00:00:51,070 --> 00:00:58,050 So inside there is going to be the cookie that we actually care about so we can extract that thing using 16 00:00:58,560 --> 00:01:03,630 response or off response that get set. 17 00:01:03,630 --> 00:01:07,210 Cookie so we are pulling off that authentication cookie. 18 00:01:07,220 --> 00:01:10,620 That is the thing that contains our Jason web token inside of it. 19 00:01:10,640 --> 00:01:15,240 We now want to take that cookie and include it in this follow up request that we're making right here. 20 00:01:15,350 --> 00:01:18,760 We can do so very easily by right after the dot get. 21 00:01:18,820 --> 00:01:22,880 We're gonna add in another change on method of set. 22 00:01:22,880 --> 00:01:26,390 We can use set to set different headers on the requests or about to make. 23 00:01:26,390 --> 00:01:32,060 So in this case we're going to set the cookie better and we'll set it to the cookie that we got from 24 00:01:32,060 --> 00:01:34,020 the initial auth response. 25 00:01:34,280 --> 00:01:38,990 They'll put in cookie like so. 26 00:01:39,050 --> 00:01:47,130 All right so let's now save this with back over to our terminal as are rerunning and there we go there 27 00:01:47,130 --> 00:01:48,470 is our current user. 28 00:01:48,510 --> 00:01:52,740 Now we could very easily write out a test and make sure that there is a current user property that is 29 00:01:52,740 --> 00:01:56,580 defined and that there's some kind of e mail defined on there as well. 30 00:01:56,590 --> 00:02:04,330 So for example I'm going to replace the console dot log right here but then expect response dot body 31 00:02:04,760 --> 00:02:09,720 not current user dot email to equal. 32 00:02:09,900 --> 00:02:13,800 And we'll have that email we'll make sure that it equals the email we had signed up as originally up 33 00:02:13,800 --> 00:02:14,500 here. 34 00:02:14,740 --> 00:02:18,970 That will say test at test dot com. 35 00:02:19,060 --> 00:02:24,940 Let's say that you know flip back over and after these tests run again we should see now current user 36 00:02:24,940 --> 00:02:27,630 is passing very good. 37 00:02:27,740 --> 00:02:28,000 All right. 38 00:02:28,010 --> 00:02:29,930 So it's headed out this approach right here works. 39 00:02:29,930 --> 00:02:36,830 But as I mentioned every other service so we test are going to have to probably initially sign up somehow. 40 00:02:36,830 --> 00:02:41,300 In other words we want to build to make authenticate requests to these other services very very easily 41 00:02:41,300 --> 00:02:42,750 in a testing environment. 42 00:02:42,930 --> 00:02:44,300 So going to take a quick pause right here. 43 00:02:44,330 --> 00:02:49,100 When we come back the next video we're going to refactor some of this authentication stuff into a helper 44 00:02:49,100 --> 00:02:50,980 function and his helper function. 45 00:02:50,990 --> 00:02:54,810 We're going to eventually kind of copy paste around to a lot of different services. 46 00:02:54,890 --> 00:02:59,480 It's going to make testing authentication and making sure we can make up their ticket requests really 47 00:02:59,480 --> 00:03:03,330 easy and straightforward so quick pause and I'll see you in just a minute.