refactoring server authentication code