'Authenticate the user, allowing the user to identify themself either by
username or email'

def _authenticate(self, username, password):

    authenticated_user = authenticate(username=username, password=password)
    if (authenticated_user is None):
        UserModel = get_user_model()
        try:
            email_user = UserModel.objects.get(email=username)
        except UserModel.DoesNotExist:
            authenticated_user = None
        else:
            authenticated_user = authenticate(username=email_user.username, password=password)
    return authenticated_user
