refactor(backends): try authenticating as superuser/admin and staff by phone first

This commit is contained in:
sankalp committed 2025-06-03 13:59:02 +05:45
1 parent 0f1f5b017c
commit cbd129df0c
1 file changed
+15
+15
View File
@@ -15,6 +15,14 @@ class MultiModelBackend(ModelBackend):
if username is None or password is None:
return None
# Try authenticating as superuser/admin first
try:
user = User.objects.get(email=username)
if user.check_password(password):
return user
except User.DoesNotExist:
pass
# Try authenticating as Staff by phone first
staff = Staff.objects.filter(phone=username).first()
if staff:
@@ -52,6 +60,13 @@ class MultiModelBackend(ModelBackend):
"""
Retrieve a user instance using their ID.
"""
# Try to get superuser/admin first
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
pass
# Try other user types
for model in [Staff, Student, Parent]:
try:
return model.objects.get(pk=user_id)