views:

114

answers:

4

i want to authenticate users using firstname and lastname

This is the code i am using

user = auth.authenticate(first_name=firstname,last_name=lastname,password=password)

it keep coming up with NoneType: None

i have checked the firstname and lastname plus password seen to be correct?

what i am doing wrong? thanks

+2  A: 

The difficulty here is that normally you'd handle this by creating a custom authentication backend that implements authenticate and get_user. However, the function signature for authenticate is:

def authenticate(self, username=None, password=None):

Everywhere in Django that would be calling this will be passing only 2 parameters, username and password. This means that using any of the generic authentication forms and things like the admin interface will break if this is done any other way.

The only work around I could see, and this is kind of sketchy, is if the username were to be typed as a single entry with a string "First Last" (delimited by a space) in place of the username. You could then separate it out and use that value...

(this is all untested, but you get the idea)

class FirstLastNameBackend(object):
    def authenticate(self, username=None, password=None):
        first, last = username.split(' ', 1)
        try:
            user = User.objects.get(first_name=first, last_name=last)
            if user:
                # Check if the password is correct
                # check if the user is active
                # etc., etc.
                return user
        except:
            pass
        return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except:
            return None

The django doc provides a lot of helpful details on doing a custom backend: User auth with custom backend

On a side note, something to be careful of is last names that have a space(s) in them, like "de la Cruz". If you specify 1 for maxsplit on the split function, you'll avoid this problem.

T. Stone
ok stone thank... but the password just is hashed?
Spikie
ah, SORRY! someone kept talking to me while I was typing that code and I forgot to include include the part where it checks to make sure the password is valid. Have a look at the django documentation link to get an idea of the whole implementation (you also need to check to make sure the user is not set to `is_active=False` and other details)
T. Stone
ok that look nice
Spikie
+2  A: 

Building a little from @T. Stone's idea. Why not have them register with their First and Last name and you just concatenate them together and use that as their username?. And everytime you have them login you setup your view to combine the two fields again and use that string.

You won't be able to use some of the auto forms they can produce for you but that's not a big deal. I'd just combine the two strings, lowercase them and slap that as the username and do the same for every login instance.

TheLizardKing
yeah i think u are making a good point
Spikie
+1  A: 

You can use any parameters in backend authentication function, i.e.:

class FirstLastNameBackend(object):
  def authenticate(self, first_name=None, last_name=None, password=None):
    pass #your user auth code goes here

In order to authenticate user you call

user = auth.authenticate(first_name=firstname,
    last_name=lastname,
    password=password)

One drawback, however, that you'll need to implement your own log in form and this authentication won't be supported in admin interface.

Yaroslav
i have tried that password was hashed
Spikie
thanks for the answer i really appreciate it
Spikie
+1  A: 

I like the idea of not making users remember a username, but I think a better solution to that is to have their email address be their user name. Is it fair for you to assume in your specific application that no two users will have the same first and last name? If that's not a fair assumption, how will your system handle that?

Ellie P.