In the last section we were able to test our entire authentication flow.
But there's still one tiny thing that we're missing to really call it complete and that is the ability
to sign out or log out of our application.
So we're going to add a tiny bit of code that will enable a user to log out.
And this really is rather straightforward.
So let's just get to it and make it happen inside of my all throughout jazz file.
I will find the two existing Google authentication routes that we've already set up underneath each
of those.
And to add one additional handler.
We're going to say that whenever a user who is authenticated makes a request to the route slash API
slash log out we will log the user out of our application.
So just like any route handler we put together first argument is the route.
The second argument is the function that we want to execute whenever a user accesses this route.
It has the arguments req and Retz.
They're short for request and response.
OK.
So just like we spoke about in the last section we had said that passport automatically attaches this
wreck and you or this user property to the wreck object.
Passport also attaches a couple of other functions to the request object as well that we can use to
manipulate the user's authentication status.
And for us the one that we care about the most is something called rec dot log out.
And I bet you can guess what that function does.
So don't log out is a function that is attached automatically to the request object by passport when
we call log out right here.
It takes the cookie that contains our users ID and it kills the ID that's in there and says OK you're
logged out you no longer have any IDs.
Any idea what user you are.
And so it just kills the cookie that's in there and says That's it.
You're not that user anymore.
Just to make sure that we send back some acknowledgement to the user saying OK you are no longer signed
in anymore we will send back red dots send and we'll just do recked user like so.
And so this is just going to prove to whoever is making this request that they are no longer signed
in.
So whenever someone makes this request to log out they should always get back something that says essentially
undefined or no content or just basically nothing at all.
So let's test this out inside the browser now and we'll be able to go through our entire authentication
flow.
So back inside the browser I'm going to kill the existing tabs I have to make a new one.
Now I'm currently considered to be logged into my application and so I'm just going to go ahead and
immediately attempt to log out by accessing localhost Kolin 5000 slash API slash log out.
Now when I do so I should see a completely empty screen.
And that means that we have gotten a response from our application but that response was empty because
we no longer have a record user.
The instant we log out we answer we call log out right here recked user is destroyed by passport because
we are no longer signed in.
So now if I tried going to API slash current user and remember we just to find that in the last section
I will also see an empty screen because rec user no longer exists.
Of course I can always sign back into the application by going to a local host call 5000 Auth. slash
Google that kicks me into the flow.
We still see this little warning message right here which we will resolve in just a little bit.
And now I can again check to see who I am so I'll do API slash current user there's my user model again.
And of course I can log out again and then get my current user again and still empty.
OK.
So that's pretty much laaga not that.
So let's continue in the next section.
And we're going to take care of a couple of odds and ends and we will also do that optional discussion
that I've been talking about nonstop at this point around some of this like cookie stuff over here as
well.
So I'll see you in just a second.