In the tutorial you provided there's a section that states:
We'll be using localStorage for persisting data to the client. This
gives us a much easier interface for persisting data across sessions
without having to deal with parsing cookies or handling cookies across
multiple domains. If a JWT token exists in localStorage, we can assume
the user is logged in as long as the token isn't expired. To log a
user out, simply remove the token from localStorage. Check out the
localStorage documentation on MDN for a better understanding of
localStorage.
If you delete a user directly from the database after you logged in with that same user, the authentication token is still present in your browser's local storage. However if you clean your browser's local storage (or just change the expiration date of the authentication token to some date in the past), you will see that if you access your app again, you won't be logged in and can't even login with that user since it was deleted from the database.
Regarding the database check, I believe the database is checked only once which is in the moment you login (i.e., to check if the user exists and the password is correct). After a successful login, the client side token is created and no more database check is needed regarding the user being logged in or not.
EDIT:
As the OP requested:
There are many ways of implementing a custom middleware. A basic one is as mentioned in How to intercept node.js express request:
var myFunc = function (req, res, next) {
// Check if user is valid
// If it is valid
next();
// If it is not valid
res.status(401);
};
app.post('/my-route', myFunc, anotherFunc);
The order of middleware placement in express routing is important.
Once a request for /my-route
is made, myFunc
will be called. In that function you can check if the user is disabled or not. If it valid "next()" will tell express to move one to the next middleware which will be anotherFunc
. If the user is not valid res.status(401);
will make express skip the other other middlware and answer that request with status code 401.