From ff9829d4d20ec1187a8fa8c69370a0256f049225 Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Thu, 2 Jun 2016 20:51:08 -0600 Subject: [PATCH] Switch back to using form-based login/logout I've decided I want this application to be a progressive enhancement application. That means that I'm not going to keep dealing with the insanity that is Javascript bundling and transpiling and building the entire UI in JS. I want to believe I can treat the web the way that it's been designed for decades - as a document platform with enhancement capabilities rather than as an emerging VM --- templates/index.html | 26 +++++++------------------- templates/layout.html | 9 +++++++++ templates/login.html | 9 +++++++++ vanth/server.py | 26 ++++++++++++++++++++------ 4 files changed, 45 insertions(+), 25 deletions(-) create mode 100644 templates/layout.html create mode 100644 templates/login.html diff --git a/templates/index.html b/templates/index.html index 18aacdc..ba1cd31 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,19 +1,7 @@ - - -
-{% if current_user and current_user.is_authenticated %} -

Hello {{ current_user.name }} from Vanth

-
- Log out -
-{% else %} -

Please log in

-
- - - Log in -
-{% endif %} - - - +{% extends 'layout.html' %} +{% block body %} +

Hello {{ current_user.name }} from Vanth

+
+ Log out +
+{% endblock %} diff --git a/templates/layout.html b/templates/layout.html new file mode 100644 index 0000000..26abc9f --- /dev/null +++ b/templates/layout.html @@ -0,0 +1,9 @@ + + +
+ {% block body %} + default stuff + {% endblock %} +
+ + diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..82be573 --- /dev/null +++ b/templates/login.html @@ -0,0 +1,9 @@ +{% extends 'layout.html' %} +{% block body %} +

Please log in

+
+ + + Log in +
+{% endblock %} diff --git a/vanth/server.py b/vanth/server.py index 8ce50f7..60318b5 100644 --- a/vanth/server.py +++ b/vanth/server.py @@ -1,3 +1,4 @@ +import logging import uuid import flask @@ -13,6 +14,8 @@ import vanth.api.user import vanth.auth import vanth.user +LOGGER = logging.getLogger(__name__) + EXPOSE_HEADERS = [ 'Location', ] @@ -21,11 +24,12 @@ def index(): return flask.render_template('index.html') def load_user(user_id): + LOGGER.debug("Loading user %s", user_id) return vanth.user.load(user_id) def login(): if flask.request.method == 'GET': - return flask.render_template('index.html') + return flask.render_template('login.html') elif flask.request.method == 'POST': user = vanth.user.load(uuid.uuid4()) flask_login.login_user(user) @@ -34,8 +38,16 @@ def login(): return flask.redirect('/') def logout(): + LOGGER.info("Logging out user %s", flask.session['user_id']) flask_login.logout_user() - return flask.redirect('/') + return flask.redirect('/login/') + +def require_login(): + LOGGER.debug("Current user %s for %s", flask.session, flask.request.path) + if flask.request.path == '/login/': + return + if not flask.session.get('user_id'): + return flask.redirect('/login/') def create_app(config): app = flask.Flask('vanth', template_folder='../templates') @@ -58,11 +70,13 @@ def create_app(config): supports_credentials=True, expose_headers=EXPOSE_HEADERS, ) - vanth.auth.register_auth_handlers(app) + #vanth.auth.register_auth_handlers(app) - app.route('/', methods=['GET'])(index) - app.route('/login/', methods=['GET', 'POST', 'DELETE'])(login) - app.route('/logout/', methods=['POST'])(logout) + app.route('/', methods=['GET'])(index) + app.route('/login/', methods=['GET', 'POST', 'DELETE'])(login) + app.route('/logout/', methods=['POST'])(logout) + + app.before_request(require_login) sepiida.endpoints.add_resource(app, vanth.api.about.About, endpoint='about') sepiida.endpoints.add_resource(app, vanth.api.ofxsource.OFXSource, endpoint='ofxsource')