1 00:00:00,980 --> 00:00:01,360 All right. 2 00:00:01,370 --> 00:00:02,000 Let's get to it. 3 00:00:02,000 --> 00:00:04,010 Let's build out this signing thing. 4 00:00:04,010 --> 00:00:09,690 But first you will do is build out that payload but again this will be a plain object that has the I.D. 5 00:00:09,870 --> 00:00:10,980 of our user. 6 00:00:10,980 --> 00:00:12,630 We can just make up an I.D. right here. 7 00:00:12,630 --> 00:00:17,780 Doesn't really matter and then put in some fake e-mail as well so put in something like test at test 8 00:00:17,780 --> 00:00:24,080 dot com after that we're going to take that payload and create a Jason web token using this as the payload 9 00:00:24,620 --> 00:00:25,990 to create the Jason wave token. 10 00:00:26,000 --> 00:00:28,490 We have to make use of that JWT library. 11 00:00:28,490 --> 00:00:29,600 Let's import that at the top. 12 00:00:29,600 --> 00:00:34,120 This file up here I'm going to import JWT from Jason. 13 00:00:34,170 --> 00:00:42,390 Web token you might recall on the JWT library there is that sign method the sign method is how we actually 14 00:00:42,390 --> 00:00:45,420 create a Jason web token when we call sign. 15 00:00:45,420 --> 00:00:49,740 We have to provide the Jason web token key and that is our key right there. 16 00:00:49,740 --> 00:00:52,950 So we'll reference process envy data to t underscore key 17 00:00:56,020 --> 00:01:00,770 Don't say token is JWT not sign. 18 00:01:00,980 --> 00:01:06,980 We're going to pass in the payload as the first argument and the second will be our process EMV JWT 19 00:01:07,070 --> 00:01:13,380 key once again typescript says hey you can't just references variable it might be undefined. 20 00:01:13,380 --> 00:01:19,320 Well we know that we've already defined it because we will have executed the before all function earlier 21 00:01:19,320 --> 00:01:19,980 on. 22 00:01:19,980 --> 00:01:22,560 So we are pretty darn sure this thing will be defined. 23 00:01:22,560 --> 00:01:27,250 So I'm going to once again adding that exclamation to say hey typescript just don't sweat it please. 24 00:01:28,960 --> 00:01:38,150 OK well then take that token and build it into our session object let's say session is Jada t token 25 00:01:40,270 --> 00:01:45,230 now we're going to turn that thing into Jason so let's call us how about session. 26 00:01:45,230 --> 00:01:51,640 Jason maybe Jason string ify session. 27 00:01:51,790 --> 00:01:53,050 Now here's kind of the hard part. 28 00:01:53,050 --> 00:01:55,940 We have to take that thing and encoded as base64. 29 00:01:56,050 --> 00:01:59,670 So how do we turn a string into base64 in no James. 30 00:02:00,010 --> 00:02:02,070 Well a little bit of complicated stuff. 31 00:02:02,080 --> 00:02:03,550 It's not really that complicated. 32 00:02:03,560 --> 00:02:04,560 Here's we're going to do. 33 00:02:04,840 --> 00:02:12,110 We'll say base64 is buffer dot from we'll pass in that session Jason. 34 00:02:12,310 --> 00:02:16,970 And then to string base 64 that's pretty much it. 35 00:02:16,970 --> 00:02:22,390 That's how we take a string and turn it into base64 encoded string in node. 36 00:02:22,590 --> 00:02:26,640 And then finally we're going to take that whole base64 encoded thing right there and make sure that 37 00:02:26,640 --> 00:02:30,690 we return it and that same kind of template we just saw it back over inside of our Chrome developer 38 00:02:30,690 --> 00:02:31,590 console. 39 00:02:31,590 --> 00:02:35,010 So we need to make sure we have expressed Collins sesh equals. 40 00:02:35,010 --> 00:02:42,010 And then the entire encoded session so we will return a template string. 41 00:02:42,010 --> 00:02:43,460 Make sure you got those back ticks. 42 00:02:43,590 --> 00:02:56,370 Express Express colon sesh equals and then base 64 that's pretty much it to not that bad. 43 00:02:56,440 --> 00:02:59,320 Now if you want to I would encourage you to go back. 44 00:02:59,320 --> 00:03:04,270 You can remove these comments and you could definitely refactor all this stuff down to like two or three 45 00:03:04,270 --> 00:03:08,380 lines of code or really two or three expressions or so if you want to do that certainly feel free to 46 00:03:08,380 --> 00:03:12,360 do so otherwise there is one other little fix we need to add in. 47 00:03:12,390 --> 00:03:14,850 You'll notice that we're now getting an air up here. 48 00:03:14,850 --> 00:03:20,370 You might recall that in order to declare a global function we had to add in this little bit of typescript 49 00:03:20,370 --> 00:03:27,590 up here so previously we had this signing function we were going to make our request inside of that 50 00:03:27,590 --> 00:03:29,710 thing to sign up and all that what not. 51 00:03:29,750 --> 00:03:33,440 And so we had to make sure that we were saying that this thing was gonna be a promise that is no longer 52 00:03:33,440 --> 00:03:33,890 the case. 53 00:03:33,890 --> 00:03:39,600 We are now just returning a string so we can go down to just that. 54 00:03:39,600 --> 00:03:46,660 Like so now one little gotcha here when we are returning this string right here. 55 00:03:46,730 --> 00:03:51,890 That definitely looks correct to us in a definitely seems to match up with what we have in cookie right 56 00:03:51,890 --> 00:03:52,520 here. 57 00:03:52,520 --> 00:03:56,660 Well unfortunately the expectation when we're using super test is that we include all of the different 58 00:03:56,660 --> 00:04:02,870 cookies in an array and so just to make super test really happy we really have to put that into an array 59 00:04:03,050 --> 00:04:09,410 like so and if we're gonna do that then we need to make sure we also update the type annotation appear 60 00:04:09,560 --> 00:04:19,940 to returning a array of strings and now my error should be all gone is it gone. 61 00:04:19,950 --> 00:04:22,650 Nope it's not it looks. 62 00:04:22,690 --> 00:04:24,180 Oh I love the async keyword on there. 63 00:04:24,190 --> 00:04:24,920 My mistake. 64 00:04:24,940 --> 00:04:26,380 That's better. 65 00:04:26,380 --> 00:04:30,660 Take off that async keyword as soon as async is on typescript things that we're returning a promise 66 00:04:30,660 --> 00:04:32,410 here we technically are returning a promise. 67 00:04:32,500 --> 00:04:34,190 We just have to take it off. 68 00:04:34,360 --> 00:04:34,620 OK. 69 00:04:34,660 --> 00:04:36,380 So that's it. 70 00:04:36,420 --> 00:04:41,950 Let's go ahead and test this thing out so we're gonna go back over to our new test file. 71 00:04:41,950 --> 00:04:45,910 We're going to find the little test right here. 72 00:04:45,910 --> 00:04:49,310 We're making sure that we did not get a 4 1 if we were signed in. 73 00:04:49,420 --> 00:04:54,490 So we need to make sure that before we make this request we attached a cookie that's going to prove 74 00:04:54,520 --> 00:05:03,940 that we are authenticated so right after hosts remember we set a cookie by calling set cookie and then 75 00:05:03,940 --> 00:05:10,030 set up here we can call sign in and it's actually the label sign in because we're trying to refer to 76 00:05:10,030 --> 00:05:12,750 a global variable. 77 00:05:12,780 --> 00:05:13,430 All right. 78 00:05:13,440 --> 00:05:14,040 Big test. 79 00:05:14,130 --> 00:05:16,880 Let's save this little makeover. 80 00:05:17,020 --> 00:05:21,330 A It's passing awesome just to make sure it really is passing. 81 00:05:21,480 --> 00:05:28,510 Let's do a console log in this test of response not status I'll say this flip back over and I'll see 82 00:05:28,600 --> 00:05:31,800 yup we definitely got through that kind of authentication wall. 83 00:05:31,950 --> 00:05:35,640 Yes that's pretty much it well simply worked out. 84 00:05:35,640 --> 00:05:39,120 Let's take a pause right here and we will move on to our remaining tests in the next video.