In this video we're going to generate a on a Web token and send it back to a user after they sign up
for our application.
So to get started at the very top of our author out Scott J.S. file I'm going to add in an additional
require statement.
I'm going to require JWT from the Jason Webb token library.
Remember we installed this library at the very start of our application then back down inside of our
request handler.
Instead of sending back a response of you made a post request right there we're going to instead create
our Jason Webb token and send that back instead.
It's on the line right above.
I'm going to say const token is JWT dot sign so this is how we create a token.
The first argument to this function is going to be the information that we want to put inside of the
token.
So in our case we're going to encode the user's I.D. so that will be user I.D. is user dot underscore
idea.
Like so then as a second argument we're going to put in our key that we're going to use to sign the
token.
Remember this key is some very special secret piece of information that we don't want to share with
the outside world.
If we share this information than anyone can create a token with any given arbitrary user I.D. so the
user or some malicious user could very easily impersonate some other person inside of application.
But right now we'll just put in a rostering right here and say like my secret key like so now of course
we would probably want you security store this piece of information and we'll figure out how to handle
that a little bit later on.
Right now we'll just use that my secret key so that's going to generate our token.
And now we can send this token back to our user.
So rather than sending back some plain text right here I'm gonna instead send back an object that has
a token property and inside there it will place our token.
Now as usual we've got an identical key and value.
So I'll condense that down to just token like so.
All right.
Let's save this and test it out.
So I going to flip back over to post man and then gonna change the email that I'm trying to sign up
with.
Remember if we tried to create a new account right now with that same email we would get a error because
we have to have a unique email.
So I'll change that e-mail to test one at test dot com and then send that out and immediately I get
a response of my token.
So that is our information right there that has encoded inside of it.
This user's I.D. So if we now include that string right there with any future request we can use that
token to validate the person making the request and make sure that they are who they say they are.
All right.
Now we've got that token right there.
Let's take a quick pause.
We start to come back the next video.
We're going to figure out how we can use that token to actually validate a user inside of some follow
up request so quick pause and I'll see you in just a minute.