refactoring middleware auth