In this video, let's implement a logic to verify a token that a user will send to our server.
So let's see how we are going to implement that inside the utils.
Let's create one file called verify token.
And here we go.
So you're going to use the same functionality here.
So we have been copied this one.
And now back to the verification token.
We can paste it here and let's change this one to verify token.
And here we are going to pass in the token that we want to verify.
So here the function we are going to pass in the token and pass in here as the agreement to the function
for this one are going to use a method called verify.
And the next argument is not this expiration, but instead a callback function that hosts the actual
user.
So here you pass in the callback function and for this we have access to error.
And then the decoded.
When we say the code, it is simply means that the user who signed in the token, we are going to have
the user property on that.
So here let's use a function here.
So let's check for error in case there is error here.
We are going to return from this function by saying that token is not valid.
So here we go return and we can say that token is not valid error.
We can go ahead and then return the actual user called the decoded.
Return the decoded user.
We can make it as decoded user or user.
The choice is yours.
So in any of a route where we want to check a token, we are going to call this function.
So let's get back to the user's root and we want to check.
And where you want to implement the authorization is the user profile.
So let's collapse this one and here we go for this one.
Unless you log in and send us a token before you can go ahead.
For the meantime, let's just verify the token.
So let's bring in the verification token here as that the auto import is working fine.
And inside the profile, which is this one we can call the function, they're going to pass in the token.
So let's assign the result from this function call as result and let's console.log the result.
So let's make the request for the meantime we need to copy the token after logging in.
So this is the token that we are waiting to pass in.
So here we go, user controller and our pass in here as the only agreement.
So moment of truth.
Let's get back to the tender client and let's locate the profile.
So for this, let's get sent and let's check our terminal here and I'm going to see something incredible
here and that is it.
You can see that we have the decoded user and this is an ID of the user core item, which is this one
when you go to log in, this is a user, let's look at the ID of the user, which is starting from six
three and end to zero, which is equal as this one.
Did you see that?
So now we have decoded the actual user.
So in case I tamper with the token, let me change something here or something here and let's hit send
and you can see that token is not valid.
So with this one, we are on our way to finish up with the authentication and then the authorization.
So what you are going to do is that we are going to create a function and then that function is going
to grab the token from the header.
So let's see how we are going to implement that one in the next video.