Albert Stepanyan
Albert Stepanyan

Reputation: 1

Do I need custom backend for django If my User model is custom?

A quick question.I had implemented a custom user model with its manager .Registration is working great ,but whenever user is logging in it shows AnonymousUser. Does this mean that I need to implement cutsom backend ot what ?? if yes could anyone give me an example of custom backend for custom user model. Kind regards EDIT:here is my settings file

  INSTALLED_APPS = [
     'django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'custom_user' ,
       ]

     ROOT_URLCONF = 'jam.urls'



     WSGI_APPLICATION = 'jam.wsgi.application'
    AUTHENTICATION_BACKENDS = ('custom_user.backends.ClientAuthBackend', 'django.contrib.auth.backends.ModelBackend')

  DATABASES = {
       'default': {
             'ENGINE': 'django.db.backends.mysql',
             'NAME':'test3',
             'USER':'root',
             'PASSWORD':'',
         }
     }

      AUTH_USER_MODEL='custom_user.EmailUser'


   LANGUAGE_CODE = 'en-us'

   TIME_ZONE = 'UTC'

   USE_I18N = True

   USE_L10N = True

   USE_TZ = True


   STATIC_URL = '/static/'

and this is backends.py

class ClientAuthBackend(object):
   def authenticate(self, username=None, password=None):
                try:
                    user = EmailUser.objects.get(email=username)
                    if EmailUser.check_password(password, user.password):
                            return user
            except EmailUser.DoesNotExist:
                    return None
    def get_user(self, user_id):
            try:
                    return EmailUser.objects.get(pk=user_id)
            except EmailUser.DoesNotExist:
                    return None

Upvotes: 0

Views: 46

Answers (1)

doniyor
doniyor

Reputation: 37876

a quick answer - no you dont need custom backend. If you want to replace the user model, you need to set AUTH_USER_MODEL = 'yourapp.YourUserModel'

custom backend is needed if you want to change something in authentication (can also be other reasons) cycle. e.g. authenticate against email instead of username etc ...

Upvotes: 1

Related Questions