Reputation: 2884
I'm using the UserModel and the UserCreationForm in Django. However, instead of requiring a username, I want to use the customer's email as the login information (i.e. completely bypass / ignore the username field).
Upvotes: 0
Views: 678
Reputation: 2116
1.) Just make the username field optional in your form and to the model pass the email as the username, after stripping out '@' from the email.
2) Create a custom authentication backend (this will accept either the username or email):
from django.contrib.auth.models import User
class EmailOrUsernameModelBackend(object):
def authenticate(self, username=None, password=None):
if '@' in username:
kwargs = {'email': username}
else:
kwargs = {'username': username}
try:
user = User.objects.get(**kwargs)
if user.check_password(password):
return user
except User.DoesNotExist:
return None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
chage your settings accordingly :
AUTHENTICATION_BACKENDS = (
'myoursite.backends.EmailOrUsernameBackend', # Custom Authentication to accept usernamee or email
)
Upvotes: 1