refactoring authentication code