So let's get started with the process of logging a user out once they're deleted, so the first thing
I want to draw here attention is this handler delete user in handler's dash API Dutko.
All this really does is delete the user from the user's table in this function right here and delete
user and that it sends back and OK, if it succeeded in doing so.
But if you look at the database and we look at the tokens table right here.
You'll see that we still have a token for user I.D., too, who has been deleted, and that's because
there's no foreign key relationship between user ID in this table and ID and the users table.
So we could if we wanted to set up a foreign key relationship and that would automatically delete this.
And that's easy enough.
Or we can go modify the code that actually takes care of that.
Now, the good thing, of course, is that because when we look up at token, we're using user ID.
This road never gets found because that user ID doesn't exist in the user's table anymore.
But we still should clean up this token since we don't need it anymore.
So let's go back and just take care of that and we'll do it at the code level, not at the database
level.
So we'll go over to delete user at this function, which is in model go.
And all I have to do here is before I return nil, let's just clean this up a little bit.
I just create another statement.
That statement equals delete from tokens where user of equals questionmark and then I execute that.
So don't care about the first result.
Do care about the error equals m db the exact context and at the context and in our statement and handed
IP and we check for an error.
So we'll just copy this code.
And of course, this can't be the assignment operator, since we already have an air base that in there
and the next time we run this, it will automatically delete the user and then it will delete any token
associated with the user.
So that's a good start.
But of course, that doesn't actually log the user out.
That's just a bit of cleanup.
So back here and handlers thought API, when this is called, how do we make sure that the user is logged
out?
Well, we could do it in the middleware.
So if we open up our middleware, which is right here under.
Command web, we have this one off we could if we wanted to.
I actually do a database look up right at this point, we have access to the necessary functions because
we have the receiver app of type pointer to application so we could at this point check for the existence
of user I.D., then read that value into it, and then check the user's table to see if that user still
exists and where we go.
And that would absolutely do the trick.
Of course, it doesn't instantly log the user out.
And ideally, what I would like to do is log that user out as soon as that user is deleted.
And of course, the other problem here is that every single time and authenticated user tries to access
something, we're doing a database lookup and those are relatively expensive in terms of system resources.
So I think we're going to take an alternate approach at this point and we'll do something using WebSocket
and we'll start that in the next election.