Using Emails to authenticate

37 views
Skip to first unread message

Frankline

unread,
Nov 4, 2012, 2:08:53 AM11/4/12
to django...@googlegroups.com
Hi all,

I guess this question has been asked here a couple of times but I'm going to ask it anyway.

I'm developing a Django application/website and I have a need to use the email for authentication instead of username. I'm more keen to find out how you handle the following:

- The default length of the email field
- Ensuring that the email field remain unique
- Making/Synchronizing the changes with the database

I'm more biased towards handling this myself rather than using the available packages out there.

Does any one have a pointer to a link on how this is handled?

Thanks.

Regards,
F. O. O.

Victor Rocha

unread,
Nov 4, 2012, 8:46:35 AM11/4/12
to django...@googlegroups.com
I haven't heard of a package that does what you need. I will try to help you as much as possible.

I use django-registration to handle user registration. It provides a form named RegistrationFormUniqueEmail, this form, as its name suggests, will ensure that the email used is unique.

For login: implement an authentication backend that makes sure that the password and email match. It should look something like this:
# Overwrite the default backend to check for e-mail address 

class EmailBackend(ModelBackend):

    def authenticate(self, username=None, password=None):
        #If username is an email address, then try to pull it up
        try:
            user = User.objects.get(email__iexact=username)
        except User.DoesNotExist:
            return None

        if user.check_password(password):
            return user

Let me know if you have any questions.
Reply all
Reply to author
Forward
0 new messages