Refactored to return errors from the auth function