So it's time to actually log the user out, the one we just deleted over WebSocket and initially I was
going to do this one way, but the more I thought about it, the more it made sense to take advantage
of all the code we wrote here in WC handlers, because we already have all the logic in place to allow
us to send a request for a WebSocket message from a page to to the server and then do something.
So what I'm going to do, first of all, is add one thing to US payload and that's the user I.D. So
let's just add user ID
and it will be Aunt and Jason.
We'll call that user underscore ID format that.
OK, so that's the first thing I'm going to do.
Now, when we go to delete a user, where do we want to send a WebSocket request to the server?
Well, I think if we go back to our application, let me start it first.
Mixter.
And go back to our Web browser and I'll just reload this page and see if I'm logged in, I don't think
I am.
Plus, so in so that example, dot com
password log in and let's see what we have for users right now.
So we'll go to all users.
They only have me.
So I'm going to add another user and I'll call this person Jack Nimble Jack at nimble dot com password
is password, and I'll save that user.
Now, if I go look at this newly created user, when I click this delete button and I click delete user,
as soon as I do that, I want to delete the user from the database and delete all the associated tokens,
if any.
And then I want to fire off that request that logs the user out of WebSocket.
So that's a place to do it.
So I'll leave cancel so we have this user to work with.
And let's go back to Visual Studio Code and let's go to that page one user, which is one dash user
page to go out and write down here.
I'll comment at this location if I want to have access to the WebSocket right from here.
I don't have access to the variable socket because back in base layout.
It only exists within this document add event listener right here, I declare it there.
So what I'll do is get rid of the statement here.
And put the last statement outside of that, so let it now it's available to me anywhere I need to use
it.
OK, so back in one user page, Doug HTML, let's go down to the elz clause.
So if data error, then I fire the error message, otherwise I'm going to redirect the user.
So at this point I've already called all users delete deleted the user and I'm all set.
When I was doing up until I commented I was just redirecting user.
So let's do this instead.
Let's uncommented because I still want to do it, but I'll just move it down a bit and get rid of those
blank spaces.
And here I'll create some data that I can send over WebSocket let and I'll just call it JSON data equal
and it'll be a JavaScript object and I'll populate the things I'm looking for on the back end.
So the action is the first thing.
And if you recall, the only action we're paying attention to in our WebSocket handlers and functions
is delete user with a capital you and I want to delete user underscore ID and I have that.
It's just ID, but I need to convert it to an end.
So person ID based temp and now that I have access to the socket variable I can type socket don't send.
Jason Stringer.
Jason did it,
and that should, in theory, work, so I believe there's one more change we need to make and that's
over in US handlers.
So if we scroll down to listen to W.S. Channel.
Here, when I get the case delete user, I actually need to send the user ID back along with it, which
is why I put it in the payload that the client sends.
So we'll say response.
The user I.D. is equal to the user ID.
OK, so let's stop our application.
Make stop, make start.
And let's give this a try, so go back to my Web browser, and first thing I'll do is go back to cancel
here.
I'll open a private window and go to localhost four thousand and log in as Jack Nimble.
So Jack nimble dot com password and I'll log in.
So Jack is now logged in and Jack is, for example, looking at all sales, and I'll just make this
window narrow enough and it's on half the screen and make this window take up the other half of the
screen.
Now, this one, I'm logged in as Jack and this one I'm logged in as admin.
So I click on Jack Nimble on all the admin users and I'll say delete and click, delete user.
And if this works.
Jack should be logged out.
And Jack is long dead, and that happened very, very quickly, so there we now have the ability to
add users, to edit users and to delete users in a very secure fashion.
Let's move on to the next section.