title: Django rest framework JWT and custom authentication backend
body: <p>I have a custom user model and have created a custom authentication backend. I am using <a href="http://www.django-rest-framework.org/" rel="noreferrer">django rest framework</a>, and <a href="http://getblimp.github.io/django-rest-framework-jwt/#extending-jsonwebtokenauthentication" rel="noreferrer">django rest framework JWT</a> for token authentication.</p>
  
  <p>User model:</p>
  
  <pre><code>class User(AbstractBaseUser, PermissionsMixin):
      email = models.EmailField(
          unique=True,
          max_length=254,
      )
      first_name = models.CharField(max_length=15)
      last_name = models.CharField(max_length=15)
      mobile = models.IntegerField(unique=True)
      date_joined = models.DateTimeField(default=timezone.now)
      is_active = models.BooleanField(default=True)
      is_admin = models.BooleanField(default=False)
      objects = UserManager()
      USERNAME_FIELD = 'email'
      REQUIRED_FIELDS = ['first_name', 'last_name', 'mobile']
  </code></pre>
  
  <p>Auth backend:</p>
  
  <pre><code>class EmailOrMobileAuthBackend(object):
      def authenticate(self, username=None, password=None):
          try:
              user = get_user_model().objects.get(email=username)
              if user.check_password(password):
                  return user
          except User.DoesNotExist:
              if username.isdigit():
                  try:
                      user = get_user_model().objects.get(mobile=username)
                      if user.check_password(password):
                          return user
                  except User.DoesNotExist:
                      return None
              else:
                  return None
  
      def get_user(self, user_id):
          try:
              return get_user_model().objects.get(pk=user_id)
          except User.DoesNotExist:
              return None
  </code></pre>
  
  <p>And have added in the settings.py:</p>
  
  <pre><code>AUTHENTICATION_BACKENDS = ('accounts.email_mobile_auth_backend.EmailOrMobileAuthBackend',)
  </code></pre>
  
  <p>While to log in to django admin site, both the email and mobile number works fine in authenticating the user. However, when I try get the token for the user using <a href="http://getblimp.github.io/django-rest-framework-jwt/#usage" rel="noreferrer">django rest framework JWT</a>, I get an error:</p>
  
  <pre><code>curl -X POST -d "email=admin@gmail.com&amp;password=123123" http://localhost/api-token-auth/
  
  "non_field_errors": [
      "Unable to log in with provided credentials."
    ]
  </code></pre>
  
  <p>I have also added the custom auth backend class in the Default authentication for Rest framework, but its still not working:</p>
  
  <pre><code>REST_FRAMEWORK = {
      ...
      'DEFAULT_AUTHENTICATION_CLASSES': (
          'accounts.email_mobile_auth_backend.EmailOrMobileAuthBackend',
          'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
      ),
  }
  </code></pre>
  
  <p>What am I missing? Why is it that its working while loggin to the django admin site, but get error when getting token with django rest framework jwt?</p>
  
  <p><strong>update</strong></p>
  
  <p>I have made another auth backend as advised and added it to the <code>DEFAULT_AUTHENTICATION_CLASSES</code>, but even this is not working.</p>
  
  <pre><code>class DrfAuthBackend(BaseAuthentication):
      def authenticate(self, username=None, password=None):
          try:
              user = get_user_model().objects.get(email=username)
              if user.check_password(password):
                  return user, None
          except User.DoesNotExist:
              if username.isdigit():
                  try:
                      user = get_user_model().objects.get(mobile=username)
                      if user.check_password(password):
                          return user, None
                  except User.DoesNotExist:
                      return None
              else:
                  return None
  </code></pre>
  
  <p>Settings:</p>
  
  <pre><code>REST_FRAMEWORK = {
      'DEFAULT_PERMISSION_CLASSES': (
          'rest_framework.permissions.IsAdminUser',
      ),
      'DEFAULT_AUTHENTICATION_CLASSES': (
          'accounts.email_mobile_auth_backend.EmailOrMobileAuthBackend',
          'accounts.email_mobile_auth_backend.DrfAuthBackend',
          'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
      ),
  }
  </code></pre>
  
  <p><strong>update</strong></p>
  
  <p>Changing the <code>args</code> in the auth class from <code>username</code> to <code>email</code> seems to work for obtaining <code>auth_token</code> but again its not working to log in to admin site.</p>
  
  <pre><code>class EmailOrMobileAuthBackend(object):
      def authenticate(self, email=None, password=None):
          try:
              user = get_user_model().objects.get(email=email)
              if user.check_password(password):
                  return user
          except User.DoesNotExist:
              if email.isdigit():
                  try:
                      user = get_user_model().objects.get(mobile=email)
                      if user.check_password(password):
                          return user
                  except User.DoesNotExist:
                      return None
              else:
                  return None
  
      def get_user(self, user_id):
          try:
              return get_user_model().objects.get(pk=user_id)
          except User.DoesNotExist:
              return None 
  </code></pre>