Factor out some redundant code in the login implementation.