refact login logic