Simplify Login mechanism code

This commit is contained in:
Patrick Jentsch 2021-09-13 17:36:19 +02:00
parent 81ed16603d
commit 05c6f1afa6

View File

@ -1,6 +1,7 @@
from datetime import datetime from datetime import datetime
from flask import abort, flash, redirect, render_template, request, url_for from flask import abort, flash, redirect, render_template, request, url_for
from flask_login import current_user, login_user, login_required, logout_user from flask_login import current_user, login_user, login_required, logout_user
from sqlalchemy import or_
from . import bp from . import bp
from .forms import (LoginForm, ResetPasswordForm, ResetPasswordRequestForm, from .forms import (LoginForm, ResetPasswordForm, ResetPasswordRequestForm,
RegistrationForm) RegistrationForm)
@ -33,10 +34,9 @@ def login():
return redirect(url_for('main.dashboard')) return redirect(url_for('main.dashboard'))
form = LoginForm(prefix='login-form') form = LoginForm(prefix='login-form')
if form.validate_on_submit(): if form.validate_on_submit():
user = User.query.filter_by(username=form.user.data).first() user = User.query.filter(or_(User.username == form.user.data,
if user is None: User.email == form.user.data.lower())).first()
user = User.query.filter_by(email=form.user.data.lower()).first() if user and user.verify_password(form.password.data):
if user is not None and user.verify_password(form.password.data):
login_user(user, form.remember_me.data) login_user(user, form.remember_me.data)
next = request.args.get('next') next = request.args.get('next')
if next is None or not next.startswith('/'): if next is None or not next.startswith('/'):