Reputation: 773
I've setup a basic authentication, but upon login function which doesnt give any error, if I return something then I get an error stating -
{'session_key': ['Session with this Session key already exists.']}
This is the code:
def header_auth(request):
auth_header = request.META['HTTP_AUTHORIZATION']
encoded_credentials = auth_header.split(' ')[1] # Removes "Basic " to isolate credentials
decoded_credentials = base64.b64decode(encoded_credentials).decode("utf-8").split(':')
return decoded_credentials[0], decoded_credentials[1]
def login_view(request):
username, password = header_auth(request)
user = authenticate(request, username=username, password=password)
if user is not None:
try:
login(request, user)
print('after login')
except Exception as e:
print('login error', e)
return HttpResponse('Authorized', status=200)
else:
return HttpResponse('Not Authorized', status=403)
def logout_view(request):
logout(request)
class FyndUser(AbstractUser):
company_id = models.IntegerField(unique=True)
If I sent the user object instead of Response then I receive the error that the user object has not attribute get.
Upvotes: 0
Views: 1060
Reputation: 1186
I ran into the same error after overriding the pre_save
signal to do a full_clean
as suggested here. For those that want to maintain the signal override, you can limit the affected models so the default Django auth still works
from django.db.models.signals import pre_save
from django.dispatch import receiver
@receiver(pre_save)
def pre_save_handler(sender, instance, *args, **kwargs):
"""Override signal to validate fields of selected models before saving."""
user_models = ['MyModel1', 'MyModel2', ...]
if sender in user_models:
instance.full_clean()
Upvotes: 1
Reputation: 773
Got the bug. I had set up a pre_save signal to one all the models to do a full_clean. So it does a full clean to all the models before saving. It was failing here -
env/lib/python3.6/site-packages/django/db/models/base.py in full_clean
line 1166 at raise ValidationError(errors)
Removing the signal worked but I still don't know what was the problem.
Upvotes: 1