From a3c219b5a3d9327f5a160e2be1d7930b0977b67b Mon Sep 17 00:00:00 2001 From: Mike Shoup Date: Sat, 6 Jul 2019 18:16:01 -0600 Subject: [PATCH] Add check box to stay logged in. --- src/humulus/auth.py | 4 +++- src/humulus/templates/auth/login.html | 3 ++- tests/test_auth.py | 9 +++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/humulus/auth.py b/src/humulus/auth.py index 5cbe2be..f8182ac 100644 --- a/src/humulus/auth.py +++ b/src/humulus/auth.py @@ -19,7 +19,7 @@ import functools from flask import (Blueprint, current_app, flash, redirect, render_template, session, url_for) from flask_wtf import FlaskForm -from wtforms import(PasswordField) +from wtforms import PasswordField, BooleanField from wtforms.validators import DataRequired @@ -29,6 +29,7 @@ bp = Blueprint('auth', __name__) class LoginForm(FlaskForm): """Form for login.""" password = PasswordField('Password', validators=[DataRequired()]) + permanent = BooleanField('Stay logged in') def login_required(view): @@ -51,6 +52,7 @@ def login(): if form.validate_on_submit(): if form.password.data == current_app.config['HUMULUS_PASSWORD']: session.clear() + session.permanent = form.permanent.data session['logged_in'] = True return redirect(url_for('index')) flash('Password is invalid.', category='warning') diff --git a/src/humulus/templates/auth/login.html b/src/humulus/templates/auth/login.html index 9570856..8a0b769 100644 --- a/src/humulus/templates/auth/login.html +++ b/src/humulus/templates/auth/login.html @@ -21,7 +21,8 @@
{{ form.hidden_tag() }}
- {{ render_field_with_errors(form.password) }} +
{{ render_field_with_errors(form.password) }}
+
{{ render_field_with_errors(form.permanent, base_class='form-check') }}
diff --git a/tests/test_auth.py b/tests/test_auth.py index a6daf9a..147b021 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -32,7 +32,16 @@ def test_login(client, auth): assert response.status_code == 302 with client.session_transaction() as session: assert session['logged_in'] + assert not session.permanent + session.clear() + # Test permanent login + data = {'password': 'password', 'permanent': 'y'} + response = client.post('/login', data=data) + assert response.status_code == 302 + with client.session_transaction() as session: + assert session['logged_in'] + assert session.permanent def test_logout(client, auth): # Login