def login():
if current_user.is_authenticated:
return redirect(url_for('index'))
form = LoginForm()
if request.method == 'POST' and form.validate_on_submit():
username = form.username.data
if '@' in username:
existing_user = User.query.filter_by(email=username).first()
else:
existing_user = User.query.filter_by(username=username).first()
if not (existing_user and existing_user.check_password(form.password.data)):
flash('Invalid username or password. Please try again.', 'warning')
return render_template('login.html', form=form)
login_user(existing_user)
db.session.add(Connection(user=existing_user, address=request.remote_addr))
db.session.commit()
return redirect(url_for('index'))
if form.errors:
flash(form.errors, 'danger')
return render_template('login.html', form=form)
评论列表
文章目录