i'm trying to log into an account that isn't superuser and getting an error from rest_framework
"detail": "No active account found with the given credentials"
using a custom user model
models.py :
lass UserAccountManager(BaseUserManager):
def create_user(self, email, first_name, last_name, password=None):
if not email:
raise ValueError('Users must have an email')
email = self.normalize_email(email)
user = self.model(email=email, first_name=first_name, last_name=last_name)
user.set_password(password)
user.save()
return user
class CustomUser(AbstractBaseUser, PermissionsMixin ):
first_name = models.CharField(unique=True, max_length=200)
last_name = models.CharField(unique=True, max_length=200)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
date_joined = models.CharField(unique=True, max_length=200)
username = models.CharField(unique=True, max_length=200)
email = models.EmailField(unique=True)
User_Roles = models.ForeignKey(User_Roles, on_delete=models.CASCADE)
USERNAME_FIELD = 'username'
objects = BaseUserManager()
i checked that the passwords are being hashed in the db but does not seem to be the case
serializers.py :
class RegisterSerializer(ModelSerializer):
password2 = serializers.CharField(style={'input_type': 'password'})
class Meta():
model = CustomUser
fields = ['username', 'email', 'first_name', 'last_name', 'password', 'password2']
def save(self):
user = CustomUser(
email=self.validated_data['email'],
username=self.validated_data['username'],
first_name=self.validated_data['first_name'],
last_name=self.validated_data['last_name'],
)
password = self.validated_data['password'],
password2 = self.validated_data['password2'],
if password != password2:
raise serializers.ValidationError({'password': 'Passwords must Match'})
user.set_password(str(password))
user.save()
return user
i had error where the password type was a tuple rather than a string or byte, and
user.set_password(str(password))
seems to have fixed the issue that i wasn't able to figure out,
the user is created and stored with a hashed password, but can't log in
CodePudding user response:
Solution
Remove the trailing commas in the following lines
password = self.validated_data['password'],
password2 = self.validated_data['password2'],
The result should be
password = self.validated_data['password']
password2 = self.validated_data['password2']
Explanation
By adding a comma, you've made it becomes a tuple which will make the error occur since the password field is a string type, not a tuple.
user.set_password(str(password))
seems to have fixed the issue that i wasn't able to figure out,
This seems to fix the issue, however, it actually will introduce another issue which is password value will be set as (PASSWORD,)
before being encrupted via set_password()
method.