refactor and test on authentication