optimize login logic