So this time around, what we want to do is to change this menu so that when a user is logged in, this
shows a log out and we want to create the necessary root and handler to actually log a user out of the
system.
And none of that is too difficult.
Now, last lecture, I said I probably use the middleware to accomplish this, and that was wrong.
I'm not going to use the middle where there's a much simpler way to do this.
I'm looking right now at the template data go file.
And if you recall, this struct holds information that is available to every single page of our Web
site.
And all I'm going to do is add a new new member to this or a new field to this.
I'm going to call it is authenticated.
Which I hope I spelled right, it will just be an.
So if this value is greater than zero, then the user will be logged in.
If it's equal to zero, then the user is not logged in.
And that's the simple logic.
So the only thing I need to do is to ensure that I populate that field every request.
And where do I do that while I do that in the render package here, the add default data.
So I need to make a decision here as to whether or not a user is logged in.
And if you recall, when the user is logged in, we set a sessional variable called User Underscore
ID.
So here in our default data, I have access to the request.
That means I can use the session.
So all I have to do is to say something like this.
If app session dot exists and give it the context, are context and look for the key user ID.
If that exists, then populated what is authenticated with one.
And that's it.
Because if it's not there, then the default value for it is authenticate.
It is the default value for an event and that's zero.
So that should give me enough to determine whether or not someone is logged in.
Now, of course, I need to still change this login to log out, and that's not that hard.
Let's go to our base layout and find the log in.
It's right here.
This is the menu item for a login.
I'll just put an if statement here.
So I'll say if equals is authenticated one, then show log out.
So we'll put a space for that.
Else show log in.
And put an end there and format this and then I'll copy this log in.
And paste it up here and change that to user log out a route that doesn't exist yet, but it will momentarily
and change this to log out.
And I guess I didn't get the pointy bracket, so there that should work, so let's try that out.
Let's run this.
Go back to her application, reload this page, log in, shows up like it should, let's put in a valid
login admin at admin dot com password and hopefully when I log in, that will change to log out.
And it does.
So it's log out.
Now, if I click on this, of course, nothing happens because that route doesn't exist yet.
So let's go create the log route, which is really, really easy.
So go back to my reads file.
I'll find the part where I've logged in and right underneath that I'll say mux dot get and I'll make
it a get request user log out and that will go to Handler's repo dot logo, which doesn't exist.
So let's go create that.
So over to our handlers file and at the very bottom, which is where I am right now, I'll create a
new function.
Which I will call funk, and it'll have to have the repository receiver.
And I'll call it logout, and because it's a handler requires a response writer.
And a pointer to a request.
And I just love the user, OK, now how do you do that?
Well, all I really need to do is to make sure that that sessional variable user ID doesn't exist.
But as this application grows, I might be putting more things in the session.
So the simplest thing to do is just to destroy the session.
And we can do that by calling our our session at UT.
So I am dot app, dot session destroyed and all that requires is the context which we can get from our
request.
And then, as I said before, there should be an equal sign there.
This throws an error that I'm not bothering to check for because destroying a session just works.
We also want to regenerate our session Tolkan or renew our session tokens.
So let's do that, too.
It's always good practice to do that.
M dot app, dot session, dot renew, Tolkan.
And that just requires the context.
And then of course we have to take them somewhere, otherwise they're just going to see a white screen.
So we'll take them back to the login screen.
Each redirect w r slash user slash login and some status code, HDTV, the status, the other and that
should work.
But let's give this its comment as we're supposed to do.
Logs a user out, so let's stop our application, start our application.
Go back to this screen, to our application, and let's reload the page, good, so we're not logged
in, so let's log in admin at admin dot com password submit.
I'm now logged in.
Let's try logging out.
And it worked.
Perfect.
Now, one thing you should be aware of, you may have noticed this already.
If I log in admin, admin, dot com password.
I'm logged in right now if I go back and stop my application and start it back up and reload this page,
I will be logged out every time you stop the application because we're just using cookies to store this
information, the login information and track our sessions.
If you stop your application and restart at the sessions are gone, you've lost your session.
And that's that's not a big deal.
When we're working, as we are right now on a local server in production, you may not want to use simple
cookies to store your session.
You may want to use something like Retests or one of the other stores that are available to us.
But that's beyond the scope of what I'm trying to teach you right now.
It's not difficult.
You can read the documentation on Alex Edwards, excession manager, and use retests or some other means
of storing your sessional information.
You can even start in the database if you want to.
And then that sort of thing won't happen because the cookies are not ephemeral.
They don't disappear between restarts of the application.
But for our purposes, this is sufficient for what we're trying to do.
So now we have a means of actually logging a user in and logging user out.
The next step is to actually create some new routes that are only available to users who are logged
in.
And we'll do that in the next lecture.