Hey there, everyone had a share and log out route is one of the most easiest route to work on.
But first, pay it small attention here and listen to my theory.
This is very important for you.
Now I see a lot of people fail in the interviews because they don't focus too much on the fundamentals.
Anybody can write a log out route, but people who actually know how the log out procedure is going
to work, they are most important for the company.
So here this out.
So far we have been working with the JWT token and one thing that you should absolutely understand about
the JWT token, they are stateless.
They doesn't really care who you are or who you are.
Not as long as anybody who is going to have this token, he can log in as long as the token is valid.
Expiry is there?
So there is no way that you can manually go ahead and change that expiry once the token is being sent
to the user.
This is not possible.
The way how you have to manage it is make sure that the token is being deleted.
Now this deletion happens of course from the cookies we are going to do it because we are sending it
to another cookie.
If you are storing these tokens into your application on the front end side, it is the responsibility
of the front end developer to remove those tokens from the local storage or wherever he is storing that
now again for the same reason, some people like to store these tokens into the back end and try to
back and forth save it.
But I think that is not an efficient way.
Every single page you are receiving a call and you are calling to the database and then comparing it
and sending it back the data.
You are just wasting a whole lot of time for that.
So make sure you just take care of these things.
And that's all I have to say on this one.
So let's go ahead and work on with this one.
We're going to just shrink this one.
And again, this is what we need.
So let's go ahead and kind of a copy paste this one.
We won't be using any of the code inside that.
But again, it makes sense sometimes to me at least to have a boilerplate up and running.
So there we go.
This one is going to be simply log out, wrapping up with the big promise.
That is all what we need now.
So how we're going to work with that now we have stored these tokens into just one place.
We need to clear them up.
So resort cookie, this is all where we are storing that.
So in the response we are going to say, Hey, you have a property known as token.
The value of this token is going to now happen as null.
That's all you have to do and you have cleared that out.
Now, apart from this, we are going to also change the expiry date as well.
So we're going to go ahead that this cookie has now expired.
So we're going to go ahead and say expires.
There we go.
Yep, looks good.
And this expires takes just the date.
So we're going to say new date and we're going to provide a date dot by that date dot now and execute
that.
So immediately the existing date will pass on there.
And apart from this, we need to pass on one more parameter which is going to be http.
So http only and we have to pass on this as true now that I said this http correctly, I guess I have
said it correctly into the helper method as well.
Let me just quickly check that cookie token and yep, it is correct.
Okay.
So that's all what we got to do.
And now once this cookie is all done, then we are going to send another response.
Now we can change on this response, but this is all we are manipulating the cookie.
We can send another response.
So we're going to go ahead and say, hey, now the status is going to be 200 and we are going to send
you a JSON response which says success is true and you have successfully logged out.
So let's provide a message as well that's going to say log, log out success.
Too many sees that figure.
So log out success.
Now, that's all it takes to log out.
Totally.
It's really simple in case you are storing it somewhere else, maybe in the database and something.
So go ahead and figure out how you are going to remove those tokens from there as well.
Now all it takes is really come up here and import this one up here.
So let's go ahead and say, hey, we need to go ahead and bring in log out, let's duplicate this one
and provide a simple route for that.
Log out.
Now, log out is not sending us anything.
So it can be a simple get request.
And the guy who is who will be handling this is going to be simply a log out.
Told you that.
Simple.
Now it's time to test this out.
So let's go ahead and move on to the postman and test this one out.
So we're going to go ahead and make a copy of this one.
We're going to call this one as log out and this one is going to be saved.
Yeah, same place.
Now, let's go ahead and change this one into a get request.
Save that and the domain is going to change to log out.
And in case you can remember that we got the cookies up here, this should be cleared out as soon as
I make a get request.
We don't need to send send any parameter.
Let's send that and then we go.
Cookies are cleared in the body.
We got the response of success.
True.
Log out.
Success totally.
From now here onwards, the things are actually really simple.
Not because they are simple.
Because we have worked so hard in the entirety of the course that things are actually looking like a
breeze.
So that is very important.
Let's go ahead and catch up in the next video.