Writing a custom user authentication with email using django

This code is to create a custom authentication which accepts email and password to login instead of username and password.

Anytime i use the "python manage.py createsuperuser" and input my details, i always get an error message which says: "return self._create_user(email, password, first_name, last_name, mobile, password, **extra_fields) TypeError: CustomUserManager._create_user() takes 6 positional arguments but 7 were given"

How do i go about this

models.py

    from django.db import models
    from datetime import datetime
    from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager


    class CustomUserManager(BaseUserManager):
        def _create_user(self, email, password, first_name, last_name, mobile, **extra_fields):
            if not email:
                raise ValueError("Email must be provided")
            if not password:
                raise ValueError("Password must be provided")
    
            user =  self.model(
                email = self.normalize_email(email),
                first_name = first_name,
                last_name = last_name,
                mobile = mobile,
                **extra_fields,
            )

            user.set_password(password)
            user.save(using=self._db)
            return user

        def create_user(self, email, password, first_name, last_name, mobile, **extra_fields):
            extra_fields.setdefault("is_active", True)
            extra_fields.setdefault("is_staff", True)
            extra_fields.setdefault("is_superuser", False)
            return self._create_user(email, password, first_name, last_name, mobile, password, **extra_fields)

        def create_superuser(self, email, password, first_name, last_name, mobile, **extra_fields):
            extra_fields.setdefault("is_active", True)
            extra_fields.setdefault("is_staff", True)
            extra_fields.setdefault("is_superuser", True)
            return self._create_user(email, password, first_name, last_name, mobile, password, **extra_fields)



    class User(AbstractBaseUser, PermissionsMixin):
        email = models.EmailField(db_index=True, unique=True, max_length=254)
        first_name = models.CharField(max_length=250)
        last_name = models.CharField(max_length=250)
        mobile = models.CharField(max_length=50)
        address = models.CharField(max_length=250)

        is_staff = models.BooleanField(default=True)
        is_superuser = models.BooleanField(default=True)
        is_superuser = models.BooleanField(default=False)

        objects = CustomUserManager()

        USERNAME_FIELD = 'email'
        REQUIRED_FIELDS = ['first_name', 'last_name', 'mobile']

        class Meta:
            verbose_name = "User"
            verbose_name_plural = "Users"

settings.py

    AUTH_USER_MODEL = "myappname.User"

These are the important parts of the code.

You're sending password twice to _create_user() in create_user() method

return self._create_user(email, password, first_name, last_name, mobile, password, **extra_fields)
Back to Top