Move user check to middleware

Remove some duplicate code in favor of a middleware.