refactored manageauth function