We just created a secret and assigned it to our pod.
So now going to open up the code tied to that container and make sure that we refer to the environment
variable that we just set inside there of JWT underscore key.
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
up root handler specifically where we are creating our Jason web token again.
We've got a hard coded string inside of here right now.
So we don't want to have this hard coded very critical piece of information side of here.
Instead we want to try to reference it from our environment variables to access the environment variable
with no J.S..
We do process not EMV.
And then the name of the variable which in our case is g deputy underscore key.
As soon as we add that in we're gonna get an error but this is coming from typescript if we hover over
it.
I'd script things that this environment variable JWT underscore key has type string or undefined.
The reason for this is that typescript is never going to assume that a enviroment variable is defined.
It is entirely possible that we're going to run our application completely forgetting to define that
environment variable.
And so typescript wants us to account for that inside of our code.
Usually what we'll do is maybe add a little bit of a check before we could say if not process not EMV
dot JWT key then throw a new error or something similar to that as soon as we add in this little type
guard right here that's going to capture the case in which J.W. t key is equal to undefined.
So if we get past that if statement typescript knows that down here.
Now this thing must be defined because we just check to see if it was defined a moment ago.
UN knows that.
Now it says hey this is a string.
Now the only downside to this is that we could possibly be throwing an area inside of a root handler
and that will only start to arise once we deploy our application.
Ideally we would capture the case in which our environment variables are not defined.
When we first start up our app.
So if we need an environment variable to get our code working correctly and it's not defined we should
throw an error at the instance our application starts up so that we know immediately.
Once we do our deployment that there's something wrong rather than trying to capture that error way
on down the line when our application has actually been running possibly for some amount of time rather
than doing the check right there.
I'm going to delete that and I'm going to go back to my index start to yes file.
So here's the index.
Yes.
I'm gonna go down to our start function and before we even attempt to connect to Auden Bongo D.D. instance
I'm going to add in a check to make sure that that environment variable actually is defined.
I'll say if not process envy JWT underscore key.
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
there's ever a scenario where I forget to do this and somehow get past the check of that secret or something
like that it's entirely possible to be honest we could completely forget to add in this section right
here if you forgot to add that in.
Then if the secret didn't exist or whatever else we would still create this pod without an issue.
So if for some reason we forget to do that then we'll capture it right here.
And we'll start to throw an error which we will detect immediately when we start to deploy our code
so I'm going to leave this check right here.
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
before.
So that little type guard that we had only works correctly the typescript if we do that check right
before we try to access that environment variable.
However we're now at a point where I personally feel pretty darn confident that if I actually started
up my application I already did a check to see if this thing is defined.
So personally I think typescript is in the wrong here.
I think typescript is being a little bit too cautious just because it doesn't know that I already did
that check when our application first started up.
So to make typescript happy here we can just add in an exclamation right after JWT key that exclamation
says hey typescript Don't worry we're ready check this thing we know 100 percent that this enviroment
variable is defined
get set looks pretty good let's save this I'm going to go back over to my terminal and just make sure
I don't have any errors during startup we would have an error if for some reason we had forgotten to
load up that enviroment variable or set it inside the container looks like everything is good so let's
go back over to postman and try to sign up once again at post man I'm going to enter in another unique
email address I'll send it and looks good there's my response source me there's my cookie and I've still
got my response on here.
All right well that is pretty much it for our initial authentication setup we've gone through this area
long process of discussing all the pros and cons of handling authentication we took a look at Jace on
web tokens for cookies we touched on Cuba net is a little bit I can get a pretty good spot so let's
take a pause right here and continue in just a moment.