Allow username for login

This commit is contained in:
Patrick Jentsch 2020-02-20 09:45:38 +01:00
parent ba3f9ec214
commit 50746ab821
5 changed files with 15 additions and 11 deletions

View File

@ -6,7 +6,7 @@ from wtforms.validators import DataRequired, Email, EqualTo, Length, Regexp
class LoginForm(FlaskForm):
email = StringField('Email', validators=[DataRequired(), Email()])
user = StringField('Email or username', validators=[DataRequired()])
password = PasswordField('Password', validators=[DataRequired()])
remember_me = BooleanField('Keep me logged in')
submit = SubmitField('Log In')

View File

@ -30,14 +30,16 @@ def login():
return redirect(url_for('main.dashboard'))
login_form = LoginForm(prefix='login-form')
if login_form.validate_on_submit():
user = User.query.filter_by(email=login_form.email.data).first()
user = User.query.filter_by(username=login_form.user.data).first()
if user is None:
user = User.query.filter_by(email=login_form.user.data).first()
if user is not None and user.verify_password(login_form.password.data):
login_user(user, login_form.remember_me.data)
next = request.args.get('next')
if next is None or not next.startswith('/'):
next = url_for('main.dashboard')
return redirect(next)
flash('Invalid email or password.')
flash('Invalid email/username or password.')
return render_template('auth/login.html.j2', login_form=login_form,
title='nopaque')

View File

@ -9,11 +9,13 @@ from . import main
def index():
login_form = LoginForm(prefix='login-form')
if login_form.validate_on_submit():
user = User.query.filter_by(email=login_form.email.data).first()
user = User.query.filter_by(username=login_form.user.data).first()
if user is None:
user = User.query.filter_by(email=login_form.user.data).first()
if user is not None and user.verify_password(login_form.password.data):
login_user(user, login_form.remember_me.data)
return redirect(url_for('main.dashboard'))
flash('Invalid email or password.')
flash('Invalid email/username or password.')
return render_template('main/index.html.j2', login_form=login_form,
title='nopaque')

View File

@ -14,9 +14,9 @@
{{ login_form.hidden_tag() }}
<div class="input-field">
<i class="material-icons prefix">person</i>
{{ login_form.email(class='validate') }}
{{ login_form.email.label }}
{% for error in login_form.email.errors %}
{{ login_form.user(class='validate') }}
{{ login_form.user.label }}
{% for error in login_form.user.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>

View File

@ -100,9 +100,9 @@
{{ login_form.hidden_tag() }}
<div class="input-field">
<i class="material-icons prefix">person</i>
{{ login_form.email(class='validate') }}
{{ login_form.email.label }}
{% for error in login_form.email.errors %}
{{ login_form.user(class='validate') }}
{{ login_form.user.label }}
{% for error in login_form.user.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>