Refactored auth functions and tests