Enable login by username.

This commit is contained in:
Patrick Jentsch 2019-07-09 11:00:41 +02:00
parent ad6201b560
commit b311fcb9de
4 changed files with 51 additions and 8 deletions

View File

@ -6,8 +6,7 @@ from ..models import User
class LoginForm(FlaskForm): class LoginForm(FlaskForm):
email = StringField('Email', validators=[DataRequired(), Length(1, 64), login = StringField('Login', validators=[DataRequired(), Length(1, 64)])
Email()])
password = PasswordField('Password', validators=[DataRequired()]) password = PasswordField('Password', validators=[DataRequired()])
remember_me = BooleanField('Keep me logged in') remember_me = BooleanField('Keep me logged in')
submit = SubmitField('Log In') submit = SubmitField('Log In')
@ -46,3 +45,9 @@ class PasswordResetRequestForm(FlaskForm):
email = StringField('Email', validators=[DataRequired(), Length(1, 64), email = StringField('Email', validators=[DataRequired(), Length(1, 64),
Email()]) Email()])
submit = SubmitField('Reset Password') submit = SubmitField('Reset Password')
class ChangeProfileForm(FlaskForm):
email = StringField('Email', validators=[DataRequired(), Length(1, 64),
Email()])
submit = SubmitField('Submit')

View File

@ -2,7 +2,7 @@ from flask import flash, redirect, render_template, request, url_for
from flask_login import current_user, login_required, login_user, logout_user from flask_login import current_user, login_required, login_user, logout_user
from . import auth from . import auth
from .. import db from .. import db
from .forms import LoginForm, PasswordResetForm, PasswordResetRequestForm, RegistrationForm from .forms import ChangeProfileForm, LoginForm, PasswordResetForm, PasswordResetRequestForm, RegistrationForm
from ..email import send_email from ..email import send_email
from ..models import User from ..models import User
@ -11,7 +11,9 @@ from ..models import User
def login(): def login():
form = LoginForm() form = LoginForm()
if form.validate_on_submit(): if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first() user = User.query.filter_by(email=form.login.data).first()
if user is None:
user = User.query.filter_by(username=form.login.data).first()
if user is not None 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')
@ -85,7 +87,7 @@ def resend_confirmation():
send_email(current_user.email, 'Confirm Your Account', 'auth/email/confirm', send_email(current_user.email, 'Confirm Your Account', 'auth/email/confirm',
user=current_user, token=token) user=current_user, token=token)
flash('A new confirmation email has benn sent to you by email.') flash('A new confirmation email has benn sent to you by email.')
return redirect(url_for('main.index')) return redirect(url_for('maind.index'))
@auth.route('/reset', methods=['GET', 'POST']) @auth.route('/reset', methods=['GET', 'POST'])
@ -121,3 +123,14 @@ def password_reset(token):
return redirect(url_for('main.index')) return redirect(url_for('main.index'))
return render_template('auth/reset_password.html.j2', form=form, return render_template('auth/reset_password.html.j2', form=form,
title='Password Reset') title='Password Reset')
@auth.route('/profile', methods=['GET', 'POST'])
@login_required
def profile():
form = ChangeProfileForm()
if form.validate_on_submit():
flash('It\'s just a test, nothing changed.')
return redirect(url_for('auth.profile'))
return render_template('auth/profile.html.j2', form=form,
title='Profile')

View File

@ -24,9 +24,9 @@
<form method="POST"> <form method="POST">
{{ form.hidden_tag() }} {{ form.hidden_tag() }}
<div class="input-field"> <div class="input-field">
<i class="material-icons prefix">email</i> <i class="material-icons prefix">person</i>
{{ form.email(class='validate', type='email') }} {{ form.login(class='validate') }}
{{ form.email.label }} {{ form.login.label }}
</div> </div>
<div class="input-field"> <div class="input-field">
<i class="material-icons prefix">vpn_key</i> <i class="material-icons prefix">vpn_key</i>

View File

@ -0,0 +1,25 @@
{% extends "base.html.j2" %}
{% block page_content %}
<div class="col s12">
<div class="card large">
<div class="card-content">
<span class="card-title">Register</span>
<form method="POST">
{{ form.hidden_tag() }}
<div class="input-field ">
<i class="material-icons prefix">email</i>
{{ form.email(class='validate', type='email') }}
{{ form.email.label }}
{% for error in form.email.errors %}
<span class="helper-text" style="color:red;">{{ error }}</span>
{% endfor %}
</div>
<div class="card-action">
{{ form.submit(class='btn right') }}
</div>
</form>
</div>
</div>
</div>
{% endblock %}