All right, and once we're familiar with the overall idea.
Now, let's actually set up our token in the create a JWT.
And this is the IBM package that I'm going to use now.
There's plenty of them out there, but this seems to be one of the most popular ones.
The name is a JSON web token.
If you want to find out more info, here is the URL.
And as always, we want to start our journey by actually installing the package.
So let me stop server.
Spin it up as well.
So let's go with and start.
I'm not in the user model.
We want to go with import.
So in my case, I'm going to go with JWT.
And then the method that we want to use in this case when we're creating is sign.
So we go JWT or taco and burrito, whatever is the name for your variable and then dot sign.
And that sign is looking for three things.
Looking for the payload, essentially what we're going to be sending.
And what's interesting in our case, we're only going to be sending the user I.D. and notice how are
accessing this?
I go with a West Dot.
Again, that's why it was important to set it up as a function and then underscore it.
So whatever you're going to be the I.D. from a user, I'll set it up here as a user already.
Now, as a quick sign out, I just prefer using this user I.D. Job ID, product I.D. I mean, can also
do something like this.
You can just say I.D. is just in my experience.
When you have a bunch of IDs and underscore I.D., it makes a little bit more sense if you're more explicit
of what this I.D. actually represents.
Just my preference if you rather prefer shake and bake.
Who am I to judge?
Now back to our main topic why we're just sending this idea.
Well, because keep in mind that we'll only access this payload back on the server.
So I really don't care about the email.
I really don't care about the name.
I really don't care about the city and whatnot.
Why?
Because I can get all of this data with my I.D. Anyway, so I'll take the 80.
I'll set up my user ID in every controller where I want to get that user.
I'll just say, Hey, look, get me that user with non-specific I.D. That's all we have to do.
Hopefully, that is clear and the rest of the stuff will send from the controller anyway.
So when we're talking about the names, emails and all that.
Yes, that will be set up, but it will be set up right from the controller when we want to pass in
the secret.
So this is going to be the value that is used to sign our tokens.
And therefore, in the next video, I'll show you where we can get more secure value and also serve
this up as Darth Envy.
And as a result, we'll have way more secure value here.
And second, we're not going to expose it when we push our code up to the game because the last thing
you want is for somebody else, start creating tokens on your behalf.
That's definitely not a place that you want to be in.
And then lastly, we have options and in the options object, we're just going to set up expires.
So that's the property.
And in my case, I'm going to go one day.
Now, if you want to find out more info, you can utilize the box because again, my assumption is that
this is a refresher for you.
Not the first time you see the JWT.
So once we have installed everything now, let's navigate to a user one.
Let's grab our JWT where I'm going to go with import JWT from.
I'm on the package name is JSON web token.
Let's scroll back down and that where we have the create a JWT.
What I want to do from this function is to return that JSON web token with payload secret for timing
just hardcoded.
Eventually, we'll set it up in the data entry and also the options where we'll set up expires then.
So let's go here with the turn JWT, then sign.
And like I said, we're going to go with User ID and then this will be equal to what this dot and underscore
it.
And just to make it clear, yes, we cannot see this idea here when we're creating schema, but we do
know that it exists, correct?
Notice the response and all the properties and what do you know all the way on the bottom?
I have underscore.
So that's going to be the party for the genre user.
And that's exactly what I'm going to set up here in my payload when we want to go with that secret again
for time being just a dummy value before we set up data entry.
And then lastly, let's go with the payload, let's say expires in and we're going to go with a one
day what service?
And here's what I want to do right now.
I want to go to auth controller.
And then instead of just invoking it like it is, I'm going to assign it to a variable in this case
token and then I want to set up one more property in this object.
So essentially, when it comes to this user, that is going to represent our user document.
Correct.
But just so it's easier on a frontend.
Just so I don't have to do some nested structuring and all that.
What I'm going to do, I'm going to add two more properties now.
The location will make sense later, and we're not going to add it.
Therefore, right now, part time being, I just want to add this token again.
In between the videos, I did remove the John, so sure and have no problems if I go back.
And what do you know, not only I have my user, but I also have my token.
And that's the one that we'll use in the communication between the frontend and the server.
So every time John is going to make some kind of request from the front end, the request will have,
unless otherwise server is going to send back the error response.