Allow users to log in with email or username

This commit is contained in:
Mouse Reeve 2021-01-18 11:01:06 -08:00
parent f825709599
commit 11dd8c8f82
3 changed files with 31 additions and 1 deletions

View file

@ -0,0 +1,22 @@
# Generated by Django 3.0.7 on 2021-01-18 19:00
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('bookwyrm', '0036_annualgoal'),
]
operations = [
migrations.AlterModelOptions(
name='shelfbook',
options={'ordering': ('-created_date',)},
),
migrations.AlterField(
model_name='user',
name='email',
field=models.EmailField(max_length=254, unique=True),
),
]

View file

@ -25,6 +25,7 @@ from . import fields, Review
class User(OrderedCollectionPageMixin, AbstractUser):
''' a user who wants to read books '''
username = fields.UsernameField()
email = models.EmailField(unique=True)
key_pair = fields.OneToOneField(
'KeyPair',

View file

@ -32,6 +32,13 @@ class Login(View):
login_form = forms.LoginForm(request.POST)
localname = login_form.data['localname']
if '@' in localname: # looks like an email address to me
email = localname
try:
username = models.User.objects.get(email=email)
except models.User.DoesNotExist: # maybe it's a full username?
username = localname
else:
username = '%s@%s' % (localname, DOMAIN)
password = login_form.data['password']
user = authenticate(request, username=username, password=password)