Refactor login flow