All right.
Let's get to it.
Let's build out this signing thing.
But first you will do is build out that payload but again this will be a plain object that has the I.D.
of our user.
We can just make up an I.D. right here.
Doesn't really matter and then put in some fake e-mail as well so put in something like test at test
dot com after that we're going to take that payload and create a Jason web token using this as the payload
to create the Jason wave token.
We have to make use of that JWT library.
Let's import that at the top.
This file up here I'm going to import JWT from Jason.
Web token you might recall on the JWT library there is that sign method the sign method is how we actually
create a Jason web token when we call sign.
We have to provide the Jason web token key and that is our key right there.
So we'll reference process envy data to t underscore key
Don't say token is JWT not sign.
We're going to pass in the payload as the first argument and the second will be our process EMV JWT
key once again typescript says hey you can't just references variable it might be undefined.
Well we know that we've already defined it because we will have executed the before all function earlier
on.
So we are pretty darn sure this thing will be defined.
So I'm going to once again adding that exclamation to say hey typescript just don't sweat it please.
OK well then take that token and build it into our session object let's say session is Jada t token
now we're going to turn that thing into Jason so let's call us how about session.
Jason maybe Jason string ify session.
Now here's kind of the hard part.
We have to take that thing and encoded as base64.
So how do we turn a string into base64 in no James.
Well a little bit of complicated stuff.
It's not really that complicated.
Here's we're going to do.
We'll say base64 is buffer dot from we'll pass in that session Jason.
And then to string base 64 that's pretty much it.
That's how we take a string and turn it into base64 encoded string in node.
And then finally we're going to take that whole base64 encoded thing right there and make sure that
we return it and that same kind of template we just saw it back over inside of our Chrome developer
console.
So we need to make sure we have expressed Collins sesh equals.
And then the entire encoded session so we will return a template string.
Make sure you got those back ticks.
Express Express colon sesh equals and then base 64 that's pretty much it to not that bad.
Now if you want to I would encourage you to go back.
You can remove these comments and you could definitely refactor all this stuff down to like two or three
lines of code or really two or three expressions or so if you want to do that certainly feel free to
do so otherwise there is one other little fix we need to add in.
You'll notice that we're now getting an air up here.
You might recall that in order to declare a global function we had to add in this little bit of typescript
up here so previously we had this signing function we were going to make our request inside of that
thing to sign up and all that what not.
And so we had to make sure that we were saying that this thing was gonna be a promise that is no longer
the case.
We are now just returning a string so we can go down to just that.
Like so now one little gotcha here when we are returning this string right here.
That definitely looks correct to us in a definitely seems to match up with what we have in cookie right
here.
Well unfortunately the expectation when we're using super test is that we include all of the different
cookies in an array and so just to make super test really happy we really have to put that into an array
like so and if we're gonna do that then we need to make sure we also update the type annotation appear
to returning a array of strings and now my error should be all gone is it gone.
Nope it's not it looks.
Oh I love the async keyword on there.
My mistake.
That's better.
Take off that async keyword as soon as async is on typescript things that we're returning a promise
here we technically are returning a promise.
We just have to take it off.
OK.
So that's it.
Let's go ahead and test this thing out so we're gonna go back over to our new test file.
We're going to find the little test right here.
We're making sure that we did not get a 4 1 if we were signed in.
So we need to make sure that before we make this request we attached a cookie that's going to prove
that we are authenticated so right after hosts remember we set a cookie by calling set cookie and then
set up here we can call sign in and it's actually the label sign in because we're trying to refer to
a global variable.
All right.
Big test.
Let's save this little makeover.
A It's passing awesome just to make sure it really is passing.
Let's do a console log in this test of response not status I'll say this flip back over and I'll see
yup we definitely got through that kind of authentication wall.
Yes that's pretty much it well simply worked out.
Let's take a pause right here and we will move on to our remaining tests in the next video.