implements a middleware to retrieve the current user when it is necessary