refactor to remove code duplication between reg and login