Flesh out login flow with password checks

This commit is contained in:
Tim Van Baak 2020-01-14 19:08:54 -08:00
parent 5031a7a5e7
commit d1fcba082f
7 changed files with 55 additions and 34 deletions

View File

@ -106,11 +106,13 @@ span.signature {
float: inherit; float: inherit;
margin: 5px auto; margin: 5px auto;
} }
div#content { div#content{
margin: 5px auto;
}
div.content-2col {
max-width: 564px; max-width: 564px;
position: static; position: static;
right: inherit; right: inherit;
margin: 5px auto;
} }
img#logo { img#logo {
max-width: inherit; max-width: inherit;

View File

@ -1,4 +1,4 @@
from flask import Blueprint, render_template, redirect, url_for from flask import Blueprint, render_template, redirect, url_for, flash
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired from wtforms.validators import DataRequired
@ -9,8 +9,8 @@ import user
class LoginForm(FlaskForm): class LoginForm(FlaskForm):
username = StringField('Username', validators=[DataRequired()]) username = StringField('Username', validators=[DataRequired()])
#password = PasswordField('Password', validators=[DataRequired()]) password = PasswordField('Password', validators=[DataRequired()])
#remember = BooleanField('Remember Me') remember = BooleanField('Stay logged in')
submit = SubmitField('Log in') submit = SubmitField('Log in')
def get_bp(login_manager): def get_bp(login_manager):
@ -27,15 +27,18 @@ def get_bp(login_manager):
if form.validate_on_submit(): if form.validate_on_submit():
username = form.username.data username = form.username.data
uid = user.uid_from_username(username) uid = user.uid_from_username(username)
if uid is None: if uid is not None:
pass u = user.user_from_uid(uid)
u = user.user_from_uid(uid) if u.check_password(form.password.data):
login_user(u) remember_me = form.remember.data
config.logger.info("Logged in user '{}' ({})".format(u.get('username'), u.uid)) login_user(u, remember=remember_me)
name = u.get('username') config.logger.info("Logged in user '{}' ({})".format(
u.get('username'), u.uid))
return redirect(url_for('home.home'))
flash("Login not recognized")
else: else:
name = "guest" pass
return render_template('auth/login.html', form=form, username=name) return render_template('auth/login.html', form=form)
@bp.route("/logout/", methods=['GET']) @bp.route("/logout/", methods=['GET'])
@login_required @login_required

View File

@ -11,8 +11,6 @@ def get_bp():
@bp.route('/', methods=['GET']) @bp.route('/', methods=['GET'])
@login_required @login_required
def home(): def home():
return render_template( return render_template('home/home.html')
'home/home.html',
sidebar_rows=[current_user.get('username'), current_user.get('displayname'), current_user.uid])
return bp return bp

View File

@ -1,11 +1,22 @@
{% extends "page_1col.html" %} {% extends "page_1col.html" %}
{% block title %}Login | Amanuensis{% endblock %} {% block title %}Login | Amanuensis{% endblock %}
{% block header %}<h2>Login</h2>{% endblock %} {% block header %}<h2>Amanuensis - Login</h2>{% endblock %}
{% block primary_content %} {% block main %}
<h1>Log in</h1>
<form action="" method="post" novalidate> <form action="" method="post" novalidate>
{{ form.hidden_tag() }} {{ form.hidden_tag() }}
<p>{{ form.username.label }}<br>{{ form.username(size=32) }}</p> <p>{{ form.username.label }}<br>{{ form.username(size=32) }}
{% for error in form.username.errors %}
<br><span style="color: #ff0000">{{ error }}</span>
{% endfor %}</p>
<p>{{ form.password.label }}<br>{{ form.password(size=32) }}
{% for error in form.password.errors %}
<br><span style="color: #ff0000">{{ error }}</span>
{% endfor %}</p>
<p>{{ form.remember }} {{ form.remember.label }}</p>
<p>{{ form.submit() }}</p> <p>{{ form.submit() }}</p>
</form> </form>
{% endblock %} {% for message in get_flashed_messages() %}
<span style="color: #ff0000">{{ message }}</span><br>
{% endfor %}
{% endblock %}
{% set template_content_blocks = [self.main()] %}

View File

@ -1,7 +1,10 @@
{% extends "page_2col.html" %} {% extends "page_2col.html" %}
{% block title %}Home | Amanuensis{% endblock %} {% block title %}Home | Amanuensis{% endblock %}
{% block header %}<h2>Amanuensis</h2>{% endblock %} {% block header %}<h2>Amanuensis - Dashboard</h2>{% endblock %}
{% block primary_content %} {% block sb_topline %}<b>{{ current_user.get('displayname') }}</b>{% endblock %}
<p><a href="{{ url_for('home.home') }}">Home</a></p> {% block sb_logout %}<a href="{{ url_for('auth.logout') }}">Log out</a>{% endblock %}
<p><a href="{{ url_for('auth.logout') }}">Logout</a></p> {% set template_sidebar_rows = [self.sb_topline(), self.sb_logout()] %}
{% endblock %} {% block main %}
<h1>Home Page</h1>
{% endblock %}
{% set template_content_blocks = [self.main()] %}

View File

@ -11,9 +11,9 @@
<div id="header">{% block header %}{% endblock %}</div> <div id="header">{% block header %}{% endblock %}</div>
{% block sidebar %}{% endblock %} {% block sidebar %}{% endblock %}
<div id="content" class="{% block content_class %}{% endblock %}"> <div id="content" class="{% block content_class %}{% endblock %}">
<div class="contentblock"> {% if not template_content_blocks %}{% set template_content_blocks = [] %}{% endif %}
{% block primary_content %}{% endblock %}</div>{% for content_block in additional_content %} {% if not content_blocks %}{% set content_blocks = [] %}{% endif %}
<div class="contentblock"> {% for content_block in template_content_blocks + content_blocks %}<div class="contentblock">
{{ content_block|safe }}</div> {{ content_block|safe }}</div>
{% endfor %} {% endfor %}
</div> </div>

View File

@ -1,8 +1,12 @@
{% extends "page.html" %} {% extends "page.html" %}
{% block sidebar %}<div id="sidebar"> {% block sidebar %}
<table>{% for row in sidebar_rows %} <div id="sidebar">
<tr><td>{{ row|safe }}</td></tr>{% endfor %} {% if not template_sidebar_rows %}{% set template_sidebar_rows = [] %}{% endif %}
</table> {% if not sidebar_rows %}{% set sidebar_rows = [] %}{% endif %}
</div> <table>
{% for row in template_sidebar_rows + sidebar_rows %}
<tr><td>{{ row|safe }}</td></tr>{% endfor %}
</table>
</div>
{% endblock %} {% endblock %}
{% block content_class %}content-2col{% endblock %} {% block content_class %}content-2col{% endblock %}