refactor the auth functions into smaller parts