Sunday, July 4, 2021

Re: Credentials fail from custom Auth backend

What does the error message say? 
guitard...@gmail.com 在 2021年7月4日 星期日下午1:48:58 [UTC+8] 的信中寫道:
Hello, I am using a custom auth backend and everything displays properly, except I cannot login with the credentials in my model. I have spent 5 hours on this and cannot figure it out. Please help.

views.py

class MyLogin(LoginView):
template_name = 'employees/login.html'
form_class = LoginForm


def form_valid(self, form):
user = form.get_user()
employee = Emp.objects.get(email=user)
if employee.is_active:
EmpBackend.authenticate(self.request, username=user, password=form.password)

return HttpResponseRedirect(self.get_success_url())

backends.py

from django.contrib.auth.backends import ModelBackend
from .models import Emp


class EmpBackend(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
try:
user = Emp.objects.get(email=username)
if user.check_password(password):
return user
except Emp.DoesNotExist:
return None

return None

def get_user(self, user_id):
try:
return Emp.objects.get(pk=user_id)
except Emp.DoesNotExist:
return None

models.py

class Emp(AbstractBaseUser):
first_name = models.CharField(max_length=100, default=None)
last_name = models.CharField(max_length=100, default=None)
email = models.EmailField(max_length=255, default=None, unique=True)
username = models.CharField(max_length=100, default=None, null=True)
phone = models.CharField(max_length=20, default=None, null=True, blank=True)
password = models.CharField(max_length=100)
address1 = models.CharField(max_length=100, default=None, null=True, blank=True)
address2 = models.CharField(max_length=100, default=None, null=True, blank=True)
city = models.CharField(max_length=100, default=None, null=True, blank=True)
state = models.CharField(max_length=100, default=None, null=True, blank=True)
zip = models.CharField(max_length=10, default=None, null=True, blank=True)
position = models.CharField(max_length=50, default=None)
date_hired = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now_add=True)
date_terminated = models.DateTimeField(default=None, null=True, blank=True)
is_active = models.BooleanField(default=True)
status = models.SmallIntegerField(default=0)
emp_is_salary = models.BooleanField(default=False)
emp_pto_rate = models.DecimalField(max_digits=8, decimal_places=4, default=0.0)
emp_user_level = models.SmallIntegerField(default=1)
emerg_contact1 = models.CharField(max_length=100, default=None, null=True, blank=True)
emerg_contact1_phone = models.CharField(max_length=20, default=None, null=True, blank=True)
emerg_contact1_address = models.CharField(max_length=200, default=None, null=True, blank=True)
emerg_contact1_city_st = models.CharField(max_length=200, default=None, null=True, blank=True)
emerg_contact2 = models.CharField(max_length=100, default=None, null=True, blank=True)
emerg_contact2_phone = models.CharField(max_length=20, default=None, null=True, blank=True)
emerg_contact2_address = models.CharField(max_length=200, default=None, null=True, blank=True)
emerg_contact2_city_st = models.CharField(max_length=200, default=None, null=True, blank=True)
emp_note = models.TextField(default=None, null=True, blank=True)
emp_hourly_rate = models.DecimalField(max_digits=8, decimal_places=4, default=0.0)
emp_net_pto = models.DecimalField(max_digits=8, decimal_places=4, default=0.0)
emp_pto_prev = models.DecimalField(max_digits=8, decimal_places=4, default=0.0)
emp_image = models.ImageField(upload_to='profile-pics/', default='profile-pics/default.png', null=True, blank=True)
last_login = models.DateTimeField(default=None, null=True)
is_staff = models.BooleanField(default=True, blank=True)
date_joined = models.DateTimeField(auto_now_add=True, blank=True)
is_superuser = models.BooleanField(default=False, blank=True)

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []

def get_full_name(self):
return f'{self.first_name} {self.last_name}'

def get_email(self):
return self.email

def __str__(self):
return self.email

def has_perm(self, perm, obj=None):
return True

def has_module_perms(self, app_label):
return True

@property
def is_staff(self):
return self.staff

@property
def is_admin(self):
return self.admin

def save(self, *args, **kwargs):
super(Emp, self).save(*args, **kwargs)

img = Image.open(self.emp_image.path)

if img.height > 300 or img.width > 300:
output_size = (300, 300)
img.thumbnail(output_size)
img.save(self.emp_image.image)

self.last_login = timezone.utc

def get_absolute_url(self):
return reverse('employees:emp-detail', args=[self.id])

objects = UserManager()

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8a2eb554-1dde-45d5-8a67-24dec60b909bn%40googlegroups.com.

No comments:

Post a Comment