Refactored login code into service