« UnicodeDecodeError Exception Fix On Templates | Home | Installing MySQLdb Without Python Egg Problems »
Authenticating Using Email vs Username
By Paul Kenjora | May 18, 2008
A few weeks back I posted the full source for user logins and password recovery under Everything A Django Developer Needs To Create Logins. There was a little bit of flack from some very passionate and smart individuals (James Bennet) insisting that this middle ware already exists in "contrib.auth;". On the other hand I received many emails thanking me for the post because it boiled down the login system for novice users. In the spirit of helping new people adopt Django and existing users improve it here is a back end modification to allow email based logins in addition to the traditional default username login.
The file: django/contrib/auth/backends.py
I started by creating a whole new backend but it turned out to be tons of copy and paste code. The solution above meets all the requirements for a backend interface and is fully compatible with all existing code. I basically created a decision function that checks if "username" or "email" was passed in as a parameter, then decided on the appropriate action. All extreme python developers please feel free to boil this down to a single line.
Now you can use two methods to authenticate, remember all imports etc… remain the same. Here are both interfaces you can use simultaneously.
user = authenticate(username=”some_username”, password=”some_password”)
user = authenticate(email=”some@emai.com”, password=”some_password”)
Thats about it, I guess the next step would be for the code to figure out if its an email or a username on the back end and pick the best one… there are pros and cons here.
PS: I’m looking for a really simple and clever way of adding OpenID to my projects. Everything I’ve found so far requires me to re-code either my site or the solution to make it sync up with my existing logins. I really just want a snippet of HTML for the templates and a single backend authentication function. I know Python OpenID libraries come in a separate directory, I want to keep them there.
Oh yeah… I’m posting the source code and templates for Everything A Django Developer Needs To Create Logins converted to new forms next week…
More from Aware Labs
- Everything A Django Developer Needs To Create Logins
- When Django Apps Grow Up
- Amazon EC2 Basics For The Curious
- Custom Actions In Django Admin Object Editor
- Google Gets Into VC Business
Aware Labs Recommends
- An Interview with Jacob Kaplan-Moss – Creator of Django (USwaretech)
- Glympse vs. Google Latitude in location sharing battle (Robert Scoble Blog)
- Django’s tipping point (Antonio Cangiano)
Topics: Model Revisions, Tutorial | Comments
-
zoltanctoth
-
cwurld
-
Brian
-
Brian
-
Brian
-
Andrew
-
Brice
-
pkenjora
-
Laundro
-
sean
-
Julian
-
NiKo