Refactor authentication to improve modularization.