I'm inside my Setup Dot T S file.
I'm at the very bottom and I'm taking a look at that sign in function.
We had originally created this function to be used back inside of our auto service.
The goal of the function was to come up with some email and password then make a request to sign up
with that email and password.
We then took the cookie out of the response and returned the cookie from the function.
Now this function right here were just fine in cyber auto service because the auto service had a root
handler for signing up unfortunately the same thing is not going to work inside of our tickets service.
The ticket service at no point time is ever going to have a route like this where we're going to try
to create some kind of account for the user.
So unfortunately we're not going to be able to use this kind of approach right here to somehow get a
cookie you might be thinking Hey during our test suite or for the tickets test suite let's just make
a request over to the off service to sign up for an account and we can get a cookie that way.
Well we definitely do not want to set up any kind of inter service dependency while we are running tests
in the words when we're running tests for the ticket service we should not try to reach out and make
requests to anything else inside our application whatsoever under any circumstance.
All tests we run side of year should be 100 percent self-contained.
So in short the code we have right here is just not going to work to somehow get a cookie but we still
need to have some ability to kind of fake being signed in inside of our test environment while we're
running these tests.
So how are we gonna do that.
Well remember this overall function the whole goal of it is to really just return a cookie and inside
that cookie is the Jason Webb token so then mind I won't go back over to the browser I want to try to
sign up for our application once again.
I want to take a look at that cookie that gets returned to us when we successfully sign up.
So back inside my browser I'm going to navigate to our application at ticketing dot Dev.
You'll notice I am not currently assigned in I'm going to go in a tent to sign up and before I sign
up I'm gonna open up my network request tab and then go to enter in some random email address and a
password after I sign up.
I can see these sign up requests and I see two requests to current user.
We do not really need to make two requests.
That is a bug that we're gonna fix up all a bit later on when we go back to the react application again.
But what's more relevant I want to take a look at those current user requests so I'll click on either
one I'm gonna go to the headers tab and I'll take a look at the request headers under request headers
is the cookie that is being sent from my browser up to the server to remember this thing right here.
That is what actually identifies us to the server.
If we could just take that information right there and send it to the server in any arbitrary request
we will be considered to be authenticated.
So ultimately that cookie that we just on the browser is what we want to return from this function.
So rather than trying to obtain a real cookie by going through the sign up process.
What if we could somehow fabricate a cookie or just make one on our own.
In this function and then return it.
Well that's exactly what we're gonna do.
We're going to build our own cookie up from scratch inside of here.
We're then going to return it from this function and then we can use that cookie to sign into our application
while we are running our tests for tickets and this process is going to be just a little bit complicated.
But to be honest to you it's going to build upon everything that we've already done around authentication.
So this is really all the same stuff we're just doing some of these steps by hand.
Instead the first thing I want to do is take the actual cookie data inside of here and just give you
a quick reminder of what's actually inside of here.
So I'm going to copy everything after express colon sesh equals so it will probably start with an E.
why I can take everything to the end I'm going to copy that and then we're gonna go to a Web site that
we took a look at a while ago Base64 decode dot org I'm going gonna paste that cookie information side
of here and then decode it so the cookie data is encoded in base64 when we decode it and get back some
actual plain text or we get back is a chase on object.
So this is not a javascript object right here.
This is Jason data.
It has the key of JWT and then the value for that is our actual Jason Webb token so with all this in
mind here's what we have to do back inside of signing.
Let's write out some comments just to guide ourselves and delete everything inside there.
I'll then replace it with a couple of comments.
The first thing I'm going to do is build a data on a Web token payload so remember for us the payload
is going to have or be an object that has an I.D. an email and was there anything else inside there.
I think there's technically also the issue that time but really for you and I we really just care about
the I.D. an email it's all we really have to do is build up that object that's pretty much it after
that we're going to create the Jason Webb token we're gonna create the Jason Webb token by running that
same kind of G2 T A SINE FUNCTION that we had put together back when we were doing our authentication
stuff back in the off middleware.
Remember when we created Jason Webb token we have to include or make use of a Jason Webb token key and
we already defined that up here inside of our before all statements
were then going to create or can build up this session objects that really just means take the Jason
Webb token We just generated and stick it into an object where it has a key of J.W. T and then a value
of the Jason one token we just built that's all that really means.
So pretty much just build an object looks like JT t my JWT we're then going to turn that session into
Jason and then after that we're going to take Jason and encode it as base 64 as we just saw because
we had to run that thing through the basics of our decode tool when our data is actually set inside
the cookie.
The cookie session Middleware is going to encode it as base64 and that's why it turns up as that kind
of long string as characters.
And then finally we're going to return a String.
That's the cookie with the encoded data.
But what I mean by this right here.
Remember in order to actually send this cookie along here's the cookie right here.
We have to take the cookie data or the real session data itself and we're gonna set it on this key right
here of express coal and sash equals.
And then that's going to be encoded cookie data so basically just build up a string that looks like
that right there OK.
So we definitely have our steps laid out for us here.
Now I know a lot of stuff might seem a little bit daunting but to be honest with you the code for this
is gonna be super straightforward really super straightforward.
It's me basically exactly each step you see right here.
Each in turn.
And as soon as we put this thing together we're gonna have a foolproof way of simulating ourselves being
authenticated in the test environment.
Quick pause here.
Build this up in just a moment.