From 50746ab82172e1a82f878e781b725af0e12f454c Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Thu, 20 Feb 2020 09:45:38 +0100 Subject: [PATCH] Allow username for login --- app/auth/forms.py | 2 +- app/auth/views.py | 6 ++++-- app/main/views.py | 6 ++++-- app/templates/auth/login.html.j2 | 6 +++--- app/templates/main/index.html.j2 | 6 +++--- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/app/auth/forms.py b/app/auth/forms.py index 43c4ae4d..d361ee07 100644 --- a/app/auth/forms.py +++ b/app/auth/forms.py @@ -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') diff --git a/app/auth/views.py b/app/auth/views.py index 1d6dcaeb..06be99cd 100644 --- a/app/auth/views.py +++ b/app/auth/views.py @@ -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') diff --git a/app/main/views.py b/app/main/views.py index d1a8636a..4af6f14c 100644 --- a/app/main/views.py +++ b/app/main/views.py @@ -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') diff --git a/app/templates/auth/login.html.j2 b/app/templates/auth/login.html.j2 index 5e793459..ff82a042 100644 --- a/app/templates/auth/login.html.j2 +++ b/app/templates/auth/login.html.j2 @@ -14,9 +14,9 @@ {{ login_form.hidden_tag() }}
person - {{ 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 %} {{ error }} {% endfor %}
diff --git a/app/templates/main/index.html.j2 b/app/templates/main/index.html.j2 index bb538490..6b97e6e9 100644 --- a/app/templates/main/index.html.j2 +++ b/app/templates/main/index.html.j2 @@ -100,9 +100,9 @@ {{ login_form.hidden_tag() }}
person - {{ 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 %} {{ error }} {% endfor %}