forked from mirrors/bookwyrm
Merge pull request #532 from mouse-reeve/email-login
Allow users to log in with email or username
This commit is contained in:
commit
da81e25022
5 changed files with 46 additions and 16 deletions
22
bookwyrm/migrations/0037_auto_20210118_1900.py
Normal file
22
bookwyrm/migrations/0037_auto_20210118_1900.py
Normal 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),
|
||||
),
|
||||
]
|
|
@ -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',
|
||||
|
|
|
@ -8,40 +8,40 @@
|
|||
{% endif %}
|
||||
<form name="edit-profile" action="/edit-profile/" method="post" enctype="multipart/form-data">
|
||||
{% csrf_token %}
|
||||
<p class="block">
|
||||
<div class="block">
|
||||
<label class="label" for="id_avatar">Avatar:</label>
|
||||
{{ form.avatar }}
|
||||
{% for error in form.avatar.errors %}
|
||||
<p class="help is-danger">{{ error | escape }}</p>
|
||||
{% endfor %}
|
||||
</p>
|
||||
<p class="block">
|
||||
</div>
|
||||
<div class="block">
|
||||
<label class="label" for="id_name">Display name:</label>
|
||||
{{ form.name }}
|
||||
{% for error in form.name.errors %}
|
||||
<p class="help is-danger">{{ error | escape }}</p>
|
||||
{% endfor %}
|
||||
</p>
|
||||
<p class="block">
|
||||
</div>
|
||||
<div class="block">
|
||||
<label class="label" for="id_summary">Summary:</label>
|
||||
{{ form.summary }}
|
||||
{% for error in form.summary.errors %}
|
||||
<p class="help is-danger">{{ error | escape }}</p>
|
||||
{% endfor %}
|
||||
</p>
|
||||
<p class="block">
|
||||
</div>
|
||||
<div class="block">
|
||||
<label class="label" for="id_email">Email address:</label>
|
||||
{{ form.email }}
|
||||
{% for error in form.email.errors %}
|
||||
<p class="help is-danger">{{ error | escape }}</p>
|
||||
{% endfor %}
|
||||
</p>
|
||||
<p class="block">
|
||||
</div>
|
||||
<div class="block">
|
||||
<label class="checkbox label" for="id_manually_approves_followers">
|
||||
Manually approve followers:
|
||||
{{ form.manually_approves_followers }}
|
||||
</label>
|
||||
</p>
|
||||
</div>
|
||||
<button class="button is-primary" type="submit">Save</button>
|
||||
</form>
|
||||
</div>
|
||||
|
@ -50,14 +50,14 @@
|
|||
<h2 class="title">Change password</h2>
|
||||
<form name="edit-profile" action="/change-password/" method="post" enctype="multipart/form-data">
|
||||
{% csrf_token %}
|
||||
<p class="block">
|
||||
<div class="block">
|
||||
<label class="label" for="id_password">New password:</label>
|
||||
<input type="password" name="password" maxlength="128" class="input" required="" id="id_password">
|
||||
</p>
|
||||
<p class="block">
|
||||
</div>
|
||||
<div class="block">
|
||||
<label class="label" for="id_confirm_password">Confirm password:</label>
|
||||
<input type="password" name="confirm-password" maxlength="128" class="input" required="" id="id_confirm_password">
|
||||
</p>
|
||||
</div>
|
||||
<button class="button is-primary" type="submit">Change password</button>
|
||||
</form>
|
||||
</div>
|
||||
|
|
|
@ -32,7 +32,14 @@ class Login(View):
|
|||
login_form = forms.LoginForm(request.POST)
|
||||
|
||||
localname = login_form.data['localname']
|
||||
username = '%s@%s' % (localname, DOMAIN)
|
||||
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)
|
||||
if user is not None:
|
||||
|
|
|
@ -185,4 +185,4 @@ class EditUser(View):
|
|||
user.save()
|
||||
|
||||
broadcast(user, user.to_update_activity(user))
|
||||
return redirect('/user/%s' % request.user.localname)
|
||||
return redirect(user.local_path)
|
||||
|
|
Loading…
Reference in a new issue