1 00:00:00,750 --> 00:00:03,920 We just created a secret and assigned it to our pod. 2 00:00:04,030 --> 00:00:09,390 So now going to open up the code tied to that container and make sure that we refer to the environment 3 00:00:09,390 --> 00:00:13,480 variable that we just set inside there of JWT underscore key. 4 00:00:13,580 --> 00:00:20,780 So going to go back into my off directory I'll find s our sea routes sign up dot TS So here's our sign 5 00:00:20,780 --> 00:00:25,280 up root handler specifically where we are creating our Jason web token again. 6 00:00:25,370 --> 00:00:28,160 We've got a hard coded string inside of here right now. 7 00:00:28,160 --> 00:00:32,080 So we don't want to have this hard coded very critical piece of information side of here. 8 00:00:32,090 --> 00:00:37,280 Instead we want to try to reference it from our environment variables to access the environment variable 9 00:00:37,310 --> 00:00:38,400 with no J.S.. 10 00:00:38,420 --> 00:00:41,280 We do process not EMV. 11 00:00:41,420 --> 00:00:46,130 And then the name of the variable which in our case is g deputy underscore key. 12 00:00:46,310 --> 00:00:51,320 As soon as we add that in we're gonna get an error but this is coming from typescript if we hover over 13 00:00:51,320 --> 00:00:52,000 it. 14 00:00:52,240 --> 00:00:59,990 I'd script things that this environment variable JWT underscore key has type string or undefined. 15 00:01:00,110 --> 00:01:05,390 The reason for this is that typescript is never going to assume that a enviroment variable is defined. 16 00:01:05,390 --> 00:01:10,430 It is entirely possible that we're going to run our application completely forgetting to define that 17 00:01:10,430 --> 00:01:11,630 environment variable. 18 00:01:11,630 --> 00:01:15,200 And so typescript wants us to account for that inside of our code. 19 00:01:15,200 --> 00:01:22,760 Usually what we'll do is maybe add a little bit of a check before we could say if not process not EMV 20 00:01:23,060 --> 00:01:32,340 dot JWT key then throw a new error or something similar to that as soon as we add in this little type 21 00:01:32,340 --> 00:01:37,890 guard right here that's going to capture the case in which J.W. t key is equal to undefined. 22 00:01:37,890 --> 00:01:41,230 So if we get past that if statement typescript knows that down here. 23 00:01:41,250 --> 00:01:45,750 Now this thing must be defined because we just check to see if it was defined a moment ago. 24 00:01:45,770 --> 00:01:46,200 UN knows that. 25 00:01:46,200 --> 00:01:48,990 Now it says hey this is a string. 26 00:01:49,060 --> 00:01:53,110 Now the only downside to this is that we could possibly be throwing an area inside of a root handler 27 00:01:53,500 --> 00:01:58,000 and that will only start to arise once we deploy our application. 28 00:01:58,000 --> 00:02:02,310 Ideally we would capture the case in which our environment variables are not defined. 29 00:02:02,380 --> 00:02:04,180 When we first start up our app. 30 00:02:04,690 --> 00:02:09,490 So if we need an environment variable to get our code working correctly and it's not defined we should 31 00:02:09,490 --> 00:02:14,000 throw an error at the instance our application starts up so that we know immediately. 32 00:02:14,020 --> 00:02:19,060 Once we do our deployment that there's something wrong rather than trying to capture that error way 33 00:02:19,060 --> 00:02:25,200 on down the line when our application has actually been running possibly for some amount of time rather 34 00:02:25,200 --> 00:02:26,800 than doing the check right there. 35 00:02:26,840 --> 00:02:32,230 I'm going to delete that and I'm going to go back to my index start to yes file. 36 00:02:32,330 --> 00:02:33,170 So here's the index. 37 00:02:33,210 --> 00:02:34,130 Yes. 38 00:02:34,380 --> 00:02:43,470 I'm gonna go down to our start function and before we even attempt to connect to Auden Bongo D.D. instance 39 00:02:43,860 --> 00:02:48,070 I'm going to add in a check to make sure that that environment variable actually is defined. 40 00:02:48,080 --> 00:02:54,140 I'll say if not process envy JWT underscore key. 41 00:02:54,150 --> 00:03:04,190 So if that thing is not defined then I'll throw a new error and say J.W. e must be defined so now if 42 00:03:04,190 --> 00:03:09,920 there's ever a scenario where I forget to do this and somehow get past the check of that secret or something 43 00:03:09,920 --> 00:03:14,540 like that it's entirely possible to be honest we could completely forget to add in this section right 44 00:03:14,540 --> 00:03:16,470 here if you forgot to add that in. 45 00:03:16,550 --> 00:03:22,670 Then if the secret didn't exist or whatever else we would still create this pod without an issue. 46 00:03:22,670 --> 00:03:26,080 So if for some reason we forget to do that then we'll capture it right here. 47 00:03:26,820 --> 00:03:31,200 And we'll start to throw an error which we will detect immediately when we start to deploy our code 48 00:03:32,820 --> 00:03:34,680 so I'm going to leave this check right here. 49 00:03:34,800 --> 00:03:39,960 I'm going to say this file and if I go back over to sign up to yes I still have the same error I had 50 00:03:39,960 --> 00:03:41,270 before. 51 00:03:41,310 --> 00:03:45,990 So that little type guard that we had only works correctly the typescript if we do that check right 52 00:03:45,990 --> 00:03:48,840 before we try to access that environment variable. 53 00:03:48,840 --> 00:03:55,260 However we're now at a point where I personally feel pretty darn confident that if I actually started 54 00:03:55,260 --> 00:03:58,890 up my application I already did a check to see if this thing is defined. 55 00:03:59,010 --> 00:04:01,410 So personally I think typescript is in the wrong here. 56 00:04:01,410 --> 00:04:05,610 I think typescript is being a little bit too cautious just because it doesn't know that I already did 57 00:04:05,610 --> 00:04:08,300 that check when our application first started up. 58 00:04:08,400 --> 00:04:15,270 So to make typescript happy here we can just add in an exclamation right after JWT key that exclamation 59 00:04:15,270 --> 00:04:20,850 says hey typescript Don't worry we're ready check this thing we know 100 percent that this enviroment 60 00:04:20,850 --> 00:04:21,920 variable is defined 61 00:04:24,770 --> 00:04:31,830 get set looks pretty good let's save this I'm going to go back over to my terminal and just make sure 62 00:04:31,830 --> 00:04:36,060 I don't have any errors during startup we would have an error if for some reason we had forgotten to 63 00:04:36,060 --> 00:04:41,160 load up that enviroment variable or set it inside the container looks like everything is good so let's 64 00:04:41,160 --> 00:04:48,890 go back over to postman and try to sign up once again at post man I'm going to enter in another unique 65 00:04:48,920 --> 00:04:56,700 email address I'll send it and looks good there's my response source me there's my cookie and I've still 66 00:04:56,700 --> 00:04:58,590 got my response on here. 67 00:04:58,800 --> 00:05:04,840 All right well that is pretty much it for our initial authentication setup we've gone through this area 68 00:05:04,840 --> 00:05:10,900 long process of discussing all the pros and cons of handling authentication we took a look at Jace on 69 00:05:10,900 --> 00:05:15,820 web tokens for cookies we touched on Cuba net is a little bit I can get a pretty good spot so let's 70 00:05:15,820 --> 00:05:18,130 take a pause right here and continue in just a moment.