From 94b78dacca1da913165ae8dc677302ed99c76789 Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Mon, 2 May 2016 11:54:32 -0600 Subject: [PATCH] Add simple user login via flask_login This doesn't do much - it'll authenticate any username and password. But at least it will show me when a user is logged in and when they aren't, which is useful to start figuring out the workflow for sessions --- templates/index.html | 14 ++++++++++++++ vanth/server.py | 38 ++++++++++++++++++++++++++++++++------ vanth/user.py | 24 ++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 templates/index.html create mode 100644 vanth/user.py diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..4ee751c --- /dev/null +++ b/templates/index.html @@ -0,0 +1,14 @@ + + +{% if current_user and current_user.is_authenticated %} +

Hello {{ current_user.name }} from Vanth

+{% else %} +

Please log in

+
+ + + Log in +
+{% endif %} + + diff --git a/vanth/server.py b/vanth/server.py index 07e64ed..2f88edc 100644 --- a/vanth/server.py +++ b/vanth/server.py @@ -1,14 +1,37 @@ -from flask import Flask -from flask_uuid import FlaskUUID -from sepiida import endpoints +import uuid + +import flask +import flask_login +import flask_uuid +import sepiida.endpoints import vanth.api.about +import vanth.user +def index(): + return flask.render_template('index.html') + +def load_user(user_id): + return vanth.user.load(user_id) + +def login(): + if flask.request.method == 'GET': + return flask.render_template('index.html') + else: + user = vanth.user.load(uuid.uuid4()) + flask_login.login_user(user) + return flask.redirect('/') + def create_app(config): - app = Flask('vanth') + app = flask.Flask('vanth', template_folder='../templates') + + flask_uuid.FlaskUUID(app) + login_manager = flask_login.LoginManager() + login_manager.init_app(app) + + login_manager.user_loader(load_user) - FlaskUUID(app) app.config.update( API_TOKEN = config.api_token, DEBUG = config.debug, @@ -16,6 +39,9 @@ def create_app(config): SESSION_COOKIE_DOMAIN = config.session_cookie_domain, ) - endpoints.add_resource(app, vanth.api.about.About, endpoint='about') + app.route('/', methods=['GET'])(index) + app.route('/login/', methods=['GET', 'POST'])(login) + + sepiida.endpoints.add_resource(app, vanth.api.about.About, endpoint='about') return app diff --git a/vanth/user.py b/vanth/user.py new file mode 100644 index 0000000..a9ef7f8 --- /dev/null +++ b/vanth/user.py @@ -0,0 +1,24 @@ +import logging + +LOGGER = logging.getLogger(__name__) + +class User(): + def __init__(self, user_id): + self.user_id = user_id + self.name = 'a person' + + def is_authenticated(self): + return True + + def is_active(self): + return True + + def is_anonymous(self): + return False + + def get_id(self): + LOGGER.debug("Getting user id") + return str(self.user_id) + +def load(user_id): + return User(user_id)