clean code and improve user authentication function