login-with-user-object-without-password-django


You don't need a password to log a user in. The auth.login function just takes a User object, which you are presumably already getting from the database when you enable the account. So you can pass that straight to login.
from django.conf import settings
from django.contrib.auth import load_backend, login


def login_user(request, user):
    """Log in a user without requiring credentials with user object"""
    if not hasattr(user, 'backend'):
        for backend in settings.AUTHENTICATION_BACKENDS:
            if user == load_backend(backend).get_user(user.pk):
                user.backend = backend
                break
    if hasattr(user, 'backend'):
        return login(request, user)