Refactoring and refinement of OAuth2 login code