Implement 'remember me' feature
This commit is contained in:
parent
f4e2689f82
commit
9f31c2cad8
|
@ -1,15 +1,14 @@
|
||||||
from flask_wtf import FlaskForm
|
from flask_wtf import FlaskForm
|
||||||
from werkzeug.security import generate_password_hash
|
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
|
from ereuse_devicehub.resources.user.models import User
|
||||||
|
|
||||||
|
|
||||||
class LoginForm(FlaskForm):
|
class LoginForm(FlaskForm):
|
||||||
email = EmailField('Email Address', [validators.Length(min=6, max=35)])
|
email = EmailField('Email Address', [validators.Length(min=6, max=35)])
|
||||||
password = PasswordField('Password', [
|
password = PasswordField('Password', [validators.DataRequired()])
|
||||||
validators.DataRequired(),
|
remember = BooleanField('Remember me')
|
||||||
])
|
|
||||||
|
|
||||||
error_messages = {
|
error_messages = {
|
||||||
'invalid_login': (
|
'invalid_login': (
|
||||||
|
|
|
@ -34,12 +34,9 @@
|
||||||
{{ form.csrf_token }}
|
{{ form.csrf_token }}
|
||||||
|
|
||||||
<div class="col-12">
|
<div class="col-12">
|
||||||
<label for="yourUsername" class="form-label">Email</label>
|
<label for="yourEmail" class="form-label">Email</label>
|
||||||
<div class="input-group has-validation">
|
<input type="email" name="email" class="form-control" id="yourEmail" required value="{{ form.email.data|default('', true) }}">
|
||||||
<span class="input-group-text" id="inputGroupPrepend">@</span>
|
<div class="invalid-feedback">Please enter your email.</div>
|
||||||
<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>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="col-12">
|
<div class="col-12">
|
||||||
|
@ -50,7 +47,7 @@
|
||||||
|
|
||||||
<div class="col-12">
|
<div class="col-12">
|
||||||
<div class="form-check">
|
<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>
|
<label class="form-check-label" for="rememberMe">Remember me</label>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -20,7 +20,7 @@ class LoginView(View):
|
||||||
# Login and validate the user.
|
# Login and validate the user.
|
||||||
# user should be an instance of your `User` class
|
# user should be an instance of your `User` class
|
||||||
user = User.query.filter_by(email=form.email.data).first()
|
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')
|
next_url = flask.request.args.get('next')
|
||||||
# is_safe_url should check if the url is safe for redirects.
|
# is_safe_url should check if the url is safe for redirects.
|
||||||
|
|
Reference in a new issue