i have created a custom user as below:
class CustomUserManager(BaseUserManager):
def create_user(self, phone_number, password=None):
if not phone_number:
raise ValueError('وارد کردن تلفن همراه ضروری میباشد.')
user = self.model(phone_number=phone_number)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, phone_number, password):
user = self.model(phone_number=phone_number)
user.set_password(password)
user.is_active = True
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
class CustomUser(AbstractBaseUser, PermissionsMixin):
username = None
name = models.CharField(max_length=30, blank=True, null=True)
phone_regex = RegexValidator(regex=r'^09\d{9}$', message="شماره تلفن را به صورت ۰۹********* وارد کنید.")
phone_number = models.CharField(max_length=11, validators=[phone_regex], unique=True)
is_staff = models.BooleanField(default=False)
USERNAME_FIELD = 'phone_number'
objects = CustomUserManager()
def __str__(self):
return self.phone_number
and a user form as below too
class UserSignupForm(forms.Form):
def __init__(self, *args, **kwargs):
super(UserSignupForm, self).__init__(*args, **kwargs)
self.fields['phone_number'].label = "شماره تلفن"
self.fields['phone_number'].widget.attrs['class'] = 'form-control input-bg-white l-field'
self.fields['password'].label = "رمز عبور"
self.fields['password'].widget.attrs['class'] = 'form-control input-bg-white l-field'
self.fields['password'].widget.attrs['id'] = 'login-signup-pass'
def clean_phone_number(self):
phone_number = self.cleaned_data['phone_number']
if CustomUser.objects.filter(phone_number=phone_number).exists():
raise forms.ValidationError("این شماره تلفن قبلا در سایت ثبت شده است.", code="already-exist")
return phone_number
phone_number = forms.CharField(
widget=forms.TextInput(attrs={'placeholder': "09123456789"}),
validators=[RegexValidator(r'^09\d{9}$', message="شماره تلفن وارد شده معتبر نمیباشد.")]
)
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = CustomUser
fields = ['phone_number', 'password']
my view:
if request.method == 'POST':
user_signup_form = UserSignupForm(request.POST)
repairman_signup_form = RepairmanSignupForm(request.POST)
if user_signup_form.is_valid() and repairman_signup_form.is_valid():
form_data = user_signup_form.cleaned_data
repairman = CustomUser(phone_number=form_data['phone_number'], password=form_data['password'])
repairman.save()
repairman.set_password(repairman.password)
repairman_profile = repairman_signup_form.save(commit=False)
# Set One to One relationship between UserForm and UserProfileInfoForm
repairman_profile.user = repairman
if 'profile_photo' in request.FILES:
repairman_profile.profile_photo = request.FILES['profile_photo']
repairman_profile.save()
phone_number=request.POST['phone_number']
password=request.POST['password']
print(phone_number, password)
new_repairman = authenticate(phone_number=phone_number, password=password)
print(new_repairman)
if new_repairman:
login(request, new_repairman)
# registered = True
return redirect('/profile')
now when i want to authenticate the user after signup, the authenticate() returns None.
what is the problem?