Search code examples
djangopython-3.xgoogle-oauth

create_user() missing 1 required positional argument: 'password'


I am authenticating using email instead of username and i trying to add google authentication everything working fine but this is the error i'm getting:

File "/home/tboss/Desktop/environment/liveimages/backend/venv/lib/python3.7/site-packages/social_django/storage.py", line 79, in create_user
user = cls.user_model().objects.create_user(*args, **kwargs)
TypeError: create_user() missing 1 required positional argument: 'password'

user model:

class UserManager(BaseUserManager):
      def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):
          if not email:
             raise ValueError('user must have email address')
          now = timezone.now()
          email = self.normalize_email(email)
          user = self.model(
                  email=email,
                  is_staff=is_staff,
                  is_active=True,
                  is_superuser=is_superuser,
                  last_login=now,
                  date_joined=now,
                  **extra_fields
                   )
         user.set_password(password)
         user.save(using=self._db)
         return user
     def create_user(self, email, password, **extra_fields):
         return self._create_user(email, password, False, False, **extra_fields)

     def create_superuser(self, email, password, **extra_fields):
         user=self._create_user(email, password, True, True, **extra_fields)
         user.save(using=self._db)
         return user

class CustomUser(AbstractBaseUser, PermissionsMixin):
      email = models.EmailField(max_length = 100, unique = True)
      First_Name = models.CharField(max_length = 100, null = True, blank = True)
      Last_Name = models.CharField(max_length = 100, null = True, blank = True)
      is_staff = models.BooleanField(default = False)
      is_superuser = models.BooleanField(default = False)
      is_active = models.BooleanField(default = True)
      last_login = models.DateTimeField(null = True, blank = True)
      date_joined = models.DateTimeField(auto_now_add=True)

      USERNAME_FIELD = "email"
      EMAIL_FIELD = "email"
      REQUIRED_FIELD = []

     objects = UserManager()

     def get_absolute_url(self):
         return "/users/%i/" % (self.pk)

Also suggest me how can i integrate with react


Solution

  • As your log says: create_user excepts a password, which is not provided by your auth method. Following changes should do the trick:

    class UserManager(BaseUserManager):
          def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):
              if not email:
                 raise ValueError('user must have email address')
              now = timezone.now()
              email = self.normalize_email(email)
              user = self.model(
                      email=email,
                      is_staff=is_staff,
                      is_active=True,
                      is_superuser=is_superuser,
                      last_login=now,
                      date_joined=now,
                      **extra_fields
                       )
            # We check if password has been given
            if password:
                user.set_password(password)
             user.save(using=self._db)
             return user
    
         #We change following functions signature to allow "No password"
         def create_user(self, email, password=None, **extra_fields):
    
             return self._create_user(email, password, False, False, **extra_fields)
    
         def create_superuser(self, email, password=None, **extra_fields):
             user=self._create_user(email, password, True, True, **extra_fields)
             user.save(using=self._db)
             return user