Middleware for keeping track of users, login states and permissions, version 2