I have a django project and i use django rest framework for my REST APIs and use JWT for authentication. I was wondering if its possible to configure JWT to authenticate users by email and password instead of username and password. I figure i can extend and override key parts of the JWT (which will be as if i was writing JWT from scratch) but i was wondering if there is a configurable way of doing so.
Thanks.
Yes, it's possible. You just have to set the USERNAME_FIELD as email (USERNAME_FIELD = 'email') and your model.
This could be an example:
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import PermissionsMixin
from django.contrib.auth.base_user import AbstractBaseUser
from django.utils.translation import ugettext_lazy as _
from .managers import UserManager
class User(AbstractBaseUser, PermissionsMixin):
    created = models.DateTimeField(_('created'), auto_now_add=True)
    email = models.EmailField(_('email'), unique=True, blank=False)
    name = models.CharField(_('name'), max_length=30, blank=False)
    last_name = models.CharField(_('last name'), max_length=100, blank=False)
    is_active = models.BooleanField(_('active'), default=True)
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['name', 'last_name']
    class Meta:
        ordering = ('created',)
        verbose_name = _('user')
        verbose_name_plural = _('users')
    def get_full_name(self):
        """
        Returns the first_name plus the last_name, with a space in between.
        """
        full_name = '%s %s' % (self.first_name, self.last_name)
        return full_name.strip()
    def get_short_name(self):
        """
        Returns the short name for the user.
        """
        return self.name
and then it should work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With