Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

Issues with custom User model - using email instead of username #2

Open
annaberglind opened this issue Oct 31, 2022 · 0 comments
Open

Comments

@annaberglind
Copy link

annaberglind commented Oct 31, 2022

Hi, thanks for this library!

I just have one small problem. My API uses a custom User model which does not have a username, only email.

I have an application using grant_type="password"
Django-oauth-toolkit supports this and if I call the django-oauth-toolkit route /o/token with username=[email protected] it understands it and I can get a token

However if I use your endpoint /auth/token I get
'User' object has no attribute 'username'

My User model for reference:

class User(AbstractBaseUser, PermissionsMixin):
    """The User model with email as a "username"."""

    email = models.EmailField(_('email address'), unique=True)
    name = models.CharField(max_length=255, blank=True) 
    is_staff = models.BooleanField(
        _('staff status'),
        default=False,
        help_text=_(
            'Designates whether the user can log into this admin site.',
        ),
    )
    is_active = models.BooleanField(
        _('active'),
        default=True,
        help_text=_(
            'Designates whether this user should be treated as active. ' +
            'Unselect this instead of deleting accounts.',
        ),
    )

    date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
    date_added = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)

    EMAIL_FIELD = 'email' 
    USERNAME_FIELD = 'email' 
    REQUIRED_FIELDS: list[str] = [] 

    objects = UserManager()

    class Meta(object):
        verbose_name = _('user')
        verbose_name_plural = _('users')

    def clean(self):
        """Normalize the email before save."""
        super().clean()
        self.email = self.__class__.objects.normalize_email(self.email)  # noqa: WPS601, E501

    def __str__(self) -> str:  # noqa: D105
        return textwrap.shorten(
            text=self.name or self.email,
            width=40,  # noqa: WPS432
            placeholder='...',
        )

Do you know any workaround for me with this problem?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant