Implement 'remember me' feature

This commit is contained in:
Santiago L 2021-12-29 08:10:26 +01:00
parent f4e2689f82
commit 9f31c2cad8
3 changed files with 8 additions and 12 deletions

View File

@ -1,15 +1,14 @@
from flask_wtf import FlaskForm
from werkzeug.security import generate_password_hash
from wtforms import EmailField, PasswordField, validators
from wtforms import BooleanField, EmailField, PasswordField, validators
from ereuse_devicehub.resources.user.models import User
class LoginForm(FlaskForm):
email = EmailField('Email Address', [validators.Length(min=6, max=35)])
password = PasswordField('Password', [
validators.DataRequired(),
])
password = PasswordField('Password', [validators.DataRequired()])
remember = BooleanField('Remember me')
error_messages = {
'invalid_login': (

View File

@ -34,12 +34,9 @@
{{ form.csrf_token }}
<div class="col-12">
<label for="yourUsername" class="form-label">Email</label>
<div class="input-group has-validation">
<span class="input-group-text" id="inputGroupPrepend">@</span>
<input type="text" name="email" class="form-control" id="yourUsername" required value="{{ form.email.data|default('', true) }}">
<div class="invalid-feedback">Please enter your username.</div>
</div>
<label for="yourEmail" class="form-label">Email</label>
<input type="email" name="email" class="form-control" id="yourEmail" required value="{{ form.email.data|default('', true) }}">
<div class="invalid-feedback">Please enter your email.</div>
</div>
<div class="col-12">
@ -50,7 +47,7 @@
<div class="col-12">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="remember" value="true" id="rememberMe">
<input class="form-check-input" type="checkbox" name="remember" {% if form.remember.data %}checked{% endif %} id="rememberMe">
<label class="form-check-label" for="rememberMe">Remember me</label>
</div>
</div>

View File

@ -20,7 +20,7 @@ class LoginView(View):
# Login and validate the user.
# user should be an instance of your `User` class
user = User.query.filter_by(email=form.email.data).first()
login_user(user)
login_user(user, remember=form.remember.data)
next_url = flask.request.args.get('next')
# is_safe_url should check if the url is safe for redirects.