0

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.

Arjunsingh
  • 703
  • 9
  • 22
  • In your `login` and `logout` views you should **Redirect to a success page** upon the operation succeeded, and what's about `FyndUser` is that a custom `User` model? – Youssef BH Jun 30 '18 at 11:31
  • yup, its a custom user model – Arjunsingh Jun 30 '18 at 11:34
  • I ust want to send a response consisting some data and not redirect – Arjunsingh Jun 30 '18 at 11:35
  • To create a custom model check out [Substituting a custom User model](https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#substituting-a-custom-user-model), you're lacking some basics. – Youssef BH Jun 30 '18 at 11:39
  • The Custome user model is correct aint it. Just adding the additional fields. mentioning auth_user_model=app.fynduser in settings Whats wrong in it? – Arjunsingh Jun 30 '18 at 11:47
  • **From Docs:** You should also define a custom manager for your user model. If your user model defines username, email, is_staff, is_active, is_superuser, last_login, and date_joined fields the same as Django’s default user, you can just install Django’s UserManager; however, if your user model defines different fields, you’ll need to define a custom manager that extends BaseUserManager providing two additional methods.... – Youssef BH Jun 30 '18 at 11:53
  • The current problem aint with the custom user as it is being stored successfully. The login also works fine but the only problem is while returning response – Arjunsingh Jun 30 '18 at 12:00

2 Answers2

1

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.

Arjunsingh
  • 703
  • 9
  • 22
1

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()
Addison Klinke
  • 1,024
  • 2
  • 14
  • 23