In this video.
Let's see how we can verify this generated token.
So back to the file.
This is what we are going to do.
We are going to create a function to verify the token.
So this is the function to generate token.
The next function is going to be point number two.
Here is going to be verify token.
And the function goes like this.
Token is equal to this function.
We take in the token that we want to verify.
So inside here we are going to make this of the package called JWT above here.
The one that we have required.
All right.
And on that, we have a method called verify and this verify fixing three argument because we are dealing
with callback here.
We can use async await, but let's make it pretty simple here by using callback here.
So you pass in the token that you're passing to this function.
And then the key that was used to sign the token and the key was this one.
The one that we use here is the key.
The second element.
And then third element is going to be the callback.
And for this callback we have access to error and then the decoded user as that and then inside the
function body, we will have access to the user.
So let's check if there is error.
It means that there is something wrong with the token.
So we can quickly return from this function.
Let's return object with some properties like this.
We can see stateless, meaning the status of the verification is false or failed.
Right.
And then we can provide a message by saying invalid.
Tolkien.
Or token expired.
You can give it a neat descriptive name.
So otherwise.
After Eve, it means that everything went on well.
And then we can go ahead and then return the decoded user.
All right, so here we go.
We can return it here.
Has decoded.
Perfect.
So let's go ahead and then run this function called verify and you pass in the token.
So this what we are going to do.
Back to the to call the token here.
This is a function that we are trying to implement.
Where is going to take the token and then verify.
And that's what this function is doing over here.
So let's see.
Let's go ahead to the root for logging in and then let's copy this token.
So let's get back to the two and let's see how we can verify this one here.
If I paste it, you can see that I got back the entire user for signing in and that is it.
And you can see that we have what is called Issue Act and Expiration Act and this is what we are going
to get inside our code.
So let me copy that and back to the file and let me paste here as an argument or I can assign to a variable.
Let me paste it inside here as the argument.
So let's save it.
And to be able to see the decoded user less console.log from this function as decoded.
When I save the file, let's check the terminal and you can see that we have the user, which is same
as the one we saw when we use the tool from JWT.
Let's look at the database here.
We have the user.
This user represents the user we use to sign meaning to generate this token and we got it back and we
have two properties ie 80 which simply means issue add and this is the date that it was issued and the
expiration ad as this one and they are in milliseconds.
So this is how we verify a token.
So what we are going to do is that after a token has been generated, then we are going to create a
murderer always to check the request that is coming in.
If a user passes this token in that way, we are going to take that token and then verify it.
So this is how we verify a token in this video.
Let's continue from here and see how we can continue to implement authentication system using this approach.