Basic login

This commit is contained in:
Emily 2019-04-26 02:28:27 +01:00
parent 49738b52eb
commit f13e18a927
8 changed files with 125 additions and 30 deletions

24
forms/login.py Normal file
View File

@ -0,0 +1,24 @@
from wtforms import Form, BooleanField, StringField, PasswordField, validators
from flask_login import UserMixin
class LoginForm(Form):
email = StringField("Email", [
validators.DataRequired(),
validators.Length(min=6, max=64)
])
password = PasswordField("Password", [
validators.DataRequired(),
validators.Length(min=6)
])
class User(UserMixin):
def __init__(self, uuid):
self.id = uuid
"""
self.name = name
self.email = email
self.password = password
"""

View File

@ -3,6 +3,7 @@ from flask import Flask, url_for, request
from objects import glob # Global sharing of python objects in a manageable way from objects import glob # Global sharing of python objects in a manageable way
glob.app = Flask(__name__) glob.app = Flask(__name__)
glob.app.secret_key = "E2FGrJXLtOxPh70Q"
import routes # All flask app routes import routes # All flask app routes
import filters # All flask app filters import filters # All flask app filters

View File

@ -1,2 +1,3 @@
Flask>=1.0.2 Flask>=1.0.2
Flask-WTF>=0.14.2 Flask-WTF>=0.14.2
flask_login>=0.4.1

View File

@ -1,16 +1,64 @@
from flask import render_template, url_for, request from flask import render_template, url_for, request, redirect, flash, abort
from wtforms import Form, BooleanField, StringField, PasswordField, validators
import flask_login
from forms.login import LoginForm, User
from objects import glob # Global sharing of python objects in a manageable way from objects import glob # Global sharing of python objects in a manageable way
login_manager = flask_login.LoginManager()
login_manager.init_app(glob.app)
login_manager.login_view = "login"
logged_in_users = []
@glob.app.route("/") @glob.app.route("/")
@glob.app.route("/home") @glob.app.route("/home")
@glob.app.route("/dashboard") @glob.app.route("/dashboard")
def home(): @flask_login.login_required
def dashboard():
return render_template("pages/dashboard.html") return render_template("pages/dashboard.html")
@glob.app.route("/login", methods = ["GET", "POST"]) @glob.app.route("/login", methods = ["GET", "POST"])
def serve_login(): def login():
if request.method == "POST": if flask_login.current_user.is_authenticated:
return "TODO: Login handle", 501 flash("Already logged in")
return render_template("login.html") return redirect(url_for("dashboard"))
form = LoginForm(request.form)
if request.method == "POST" and form.validate():
# TODO: get uuid from database
uuid = 0
if uuid < 0:
flash("Invalid login")
return render_template("login.html", form=form)
user = User(uuid)
flask_login.login_user(user)
logged_in_users.append(user)
flash("Logged in")
return redirect(url_for("dashboard"))
return render_template("login.html", form=form)
@glob.app.route("/logout")
@flask_login.login_required
def logout():
flask_login.logout_user()
flash("Logged out")
return redirect(url_for("login"))
@glob.app.errorhandler(401)
def unauthorized_handler_err():
flash("Login is required")
unauthorized_handler()
@login_manager.user_loader
def load_user(uuid):
return User(uuid)
@login_manager.unauthorized_handler
def unauthorized_handler():
return redirect(url_for("login"))

View File

@ -10,4 +10,5 @@ html, body {
background-color: white; background-color: white;
border-radius: 6px; border-radius: 6px;
padding: 8px; padding: 8px;
} }

View File

@ -140,13 +140,13 @@
</div> </div>
<div> <div>
<a class="item" onclick="toggleCategory(this)"> <a class="item" onclick="toggleCategory(this)">
<i class="fa fa-chart-bar"></i><span>Recieps</span> <i class="fa fa-chart-bar"></i><span>Receipt</span>
<i class="fas fa-chevron-right"></i> <i class="fas fa-chevron-right"></i>
</a> </a>
<div class="downtab hidden"> <div class="downtab hidden">
<div class="flex-column"> <div class="flex-column">
<div class="page"> <div class="page">
<a href="#recieps-recieps">Recieps</a> <a href="#recieps-recieps">Receipt</a>
</div> </div>
<div class="page"> <div class="page">
<a href="#recieps-log">Log</a> <a href="#recieps-log">Log</a>

View File

@ -3,6 +3,8 @@
{% extends "layout/bootstrap.html" %} {% extends "layout/bootstrap.html" %}
{% block content %} {% block content %}
<script src="{{ url_for('static', filename='js/alerts.js') }}"></script>
<div class="container pt-3"> <div class="container pt-3">
<div class="row alert-section"> <div class="row alert-section">
<div class="col-md-8 mx-auto"> <div class="col-md-8 mx-auto">
@ -32,11 +34,46 @@
<a class="btn btn-primary btn-lg btn-block" href="#TODO_LOGIN_EID" onclick="alertAbove(this, 'warning', 'This should redirect the user to Electronic ID page (but due to phishing, we can not demo this)')">Electronic ID</a> <a class="btn btn-primary btn-lg btn-block" href="#TODO_LOGIN_EID" onclick="alertAbove(this, 'warning', 'This should redirect the user to Electronic ID page (but due to phishing, we can not demo this)')">Electronic ID</a>
</div> </div>
<div class="col-md-6"> <div class="col-md-6">
<a class="btn btn-primary btn-lg btn-block" id="test" href="#TODO_LOGIN_HA" onclick="alertAbove(this, 'info', 'Unimplemented - Awaiting database')">House Account</a> <a class="btn btn-primary btn-lg btn-block" href="#" data-toggle="modal" data-target="#myModal">House Account</a>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Login</h4>
<button type="button" class="close" data-dismiss="modal">&times;</button>
</div>
<div class="modal-body">
{% macro render_field(field) %}
<dt>{{ field.label }}
<dd>{{ field(**kwargs)|safe }}
{% if field.errors %}
<ul class=errors>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
</dd>
{% endmacro %}
<form method=post>
<dl>
{{ render_field(form.email) }}
{{ render_field(form.password) }}
</dl>
<input type=submit value="Login">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div> </div>
{% endblock %} {% endblock %}

View File

@ -53,24 +53,6 @@ $(document).ready( function() {
}, },
onMonthChange: function () { onMonthChange: function () {
console.log('Cal-1 month changed'); console.log('Cal-1 month changed');
},
nextYear: function () {
console.log('Cal-1 next year');
},
previousYear: function () {
console.log('Cal-1 previous year');
},
onYearChange: function () {
console.log('Cal-1 year changed');
},
nextInterval: function () {
console.log('Cal-1 next interval');
},
previousInterval: function () {
console.log('Cal-1 previous interval');
},
onIntervalChange: function () {
console.log('Cal-1 interval changed');
} }
}, },
multiDayEvents: { multiDayEvents: {
@ -79,7 +61,8 @@ $(document).ready( function() {
startDate: 'startDate' startDate: 'startDate'
}, },
showAdjacentMonths: true, showAdjacentMonths: true,
adjacentDaysChangeMonth: false adjacentDaysChangeMonth: false,
weekOffset: 1
}); });
// Bind all clndrs to the left and right arrow keys // Bind all clndrs to the left and right arrow keys