Thursday, September 28, 2017

Problem testing user is None when extending auth backend.

In a custom authentication backend, I was getting this error with this (previous developer's!) code:
   if user.is_authenticated() and user.is_staff:  AttributeError: 'NoneType' object has no attribute 'is_authenticated'

The cause seemed clear, the parent classes method was run first with super and returned None when it failed to authenticate, so I tried to fix by testing whether user is None, but I still get this:

if (user is not None) and user.is_authenticated() and user.is_staff:  AttributeError: 'NoneType' object has no attribute 'is_authenticated'

I am probably missing something obvious, but if user is None, only (user is not None) will be evaluated there should be no error, if user is not None, I should not have a NoneType in the error.

--
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/dee49d27-713d-4035-9e8c-4e7b20850856%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment