0

I am using django-mongoengine to use mongodb with django. I manually created a document containing user_data in user collection manually like this:

The user and django_session were created automatically when I integrated the module and ran the project

Now I tried to authenticate user like this:

def loginAction(request):
    if request.is_ajax():
        email = request.POST.get('email')
        password = request.POST.get('password')

        user = authenticate(email=email, password=password)
        print user
        if user:
            if user.is_authenticated():
                login(request, user)
                return HttpResponse(json.dumps({'RESULT':'SUCCESS'}),content_type="application/json")
        else:
            return HttpResponse(json.dumps({'RESULT':'FAILURE'}),content_type="application/json")

However It is returning None when I am trying to print user. I tried several codes but i am still not able to authenticate.

My settings.py :

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_mongoengine',
    'django_mongoengine.mongo_auth',
    'login_app',
]

MONGODB_DATABASES = {
    'default': {'name': 'picknbox'}
}


DATABASES = {
    'default': {'ENGINE': 'django.db.backends.dummy'}
}

AUTH_USER_MODEL = 'mongo_auth.MongoUser'

AUTHENTICATION_BACKENDS = (
    'django_mongoengine.mongo_auth.backends.MongoEngineBackend',
)

SESSION_ENGINE = 'django_mongoengine.sessions'

My question is how to authenticate using the django-mongoengine backend?

Note: I tried the same approach with sqlite database using custom backend and it worked. I was able to authenticate as well as login and logout was working fine.

EDIT: I checked the MongoEngineBackend class in AUTHENTICATION_BACKENDS and it is defined as:

from django.contrib import auth


class MongoEngineBackend(object):
    """Authenticate using MongoEngine and mongoengine.django.auth.User.
    """

    supports_object_permissions = False
    supports_anonymous_user = False
    supports_inactive_user = False

    authenticate = auth.backends.ModelBackend.__dict__["authenticate"]
    get_user = auth.backends.ModelBackend.__dict__["get_user"]

so it uses the django's auth.

Manish Gupta
  • 4,438
  • 18
  • 57
  • 104

0 Answers0