refactor login logic