I have successfully implemented my auth/login custom backend, and now i am trying to implement my own django-registration custom backend. The django-registration code seems to work fine, if i use the normal authentication from contrib.auth . If not (in my case i want to use my own newly created custom authentication) i get an
User object has no attribute backend
.
my registration backend:
from django.conf import settings
#from django.contrib.auth import authenticate
from django.contrib.auth import login
from registration import signals
from registration.forms import RegistrationForm
class MyRegistrationBackend(object):
def register(self, request, **kwargs):
"""
Create and immediately log in a new user.
"""
print "debug"
username, email, password = kwargs['username'], kwargs['email'], kwargs['password1']
User.objects.create_user(username, email, password)
# authenticate() always has to be called before login(), and
# will return the user we just created.
auth = MyAuthBackend()
new_user = auth.authenticate(username=username, password=password)
login(request, new_user)
signals.user_registered.send(sender=self.__class__,
user=new_user,
request=request)
return new_user
then my auth backend:
class MyAuthBackend(object):
"""
Authenticates against django.contrib.auth.models.User. with my modifications
"""
supports_inactive_user = True
"""
This function does not upgrade the user password hasher
"""
def check_password(self,password, encoded):
if not password or not is_password_usable(encoded):
return False
password = smart_str(password)
encoded = smart_str(encoded)
if encoded[0] == "$":
encoded = encoded[1:] #make it compatible so that drupal 7 sha512 hasher can work properly
if len(encoded) == 32 and '$' not in encoded:
hasher = get_hasher('unsalted_md5')
else:
algorithm = encoded.split('$', 1)[0]
hasher = get_hasher(algorithm)
is_correct = hasher.verify(password, encoded)
return is_correct
def authenticate(self, username=None, password=None):
try:
user = User.objects.get(username=username)
if self.check_password(password, user.password):
return user
except User.DoesNotExist:
return None
Any ideas?? I believe that maybe i am instantiating the auth = MyAuthBackend()
the wrong way.. or maybe its something else
Try setting the AUTHENTICATION_BACKENDS
setting as described in the docs on specifying authentication backends
Then, in your register method, use the django.contrib.auth.authenticate
method to log in (see how to log a user in) instead of instantiating an instance of your backend manually. That authenticate method should take care of setting the user backend
attribute, so you shouldn't get any errors.