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
This commit is contained in:
parent
ccde1908ba
commit
94b78dacca
|
@ -0,0 +1,14 @@
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
{% if current_user and current_user.is_authenticated %}
|
||||||
|
<h1>Hello {{ current_user.name }} from Vanth</h1>
|
||||||
|
{% else %}
|
||||||
|
<h1>Please log in</h1>
|
||||||
|
<form action="/login/" method="POST">
|
||||||
|
<label>username</label><input type="text" name="username"></input>
|
||||||
|
<label>password</label><input type="password" name="password"></input>
|
||||||
|
<input type="submit">Log in</input>
|
||||||
|
</form>
|
||||||
|
{% endif %}
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -1,14 +1,37 @@
|
||||||
from flask import Flask
|
import uuid
|
||||||
from flask_uuid import FlaskUUID
|
|
||||||
from sepiida import endpoints
|
import flask
|
||||||
|
import flask_login
|
||||||
|
import flask_uuid
|
||||||
|
import sepiida.endpoints
|
||||||
|
|
||||||
import vanth.api.about
|
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):
|
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(
|
app.config.update(
|
||||||
API_TOKEN = config.api_token,
|
API_TOKEN = config.api_token,
|
||||||
DEBUG = config.debug,
|
DEBUG = config.debug,
|
||||||
|
@ -16,6 +39,9 @@ def create_app(config):
|
||||||
SESSION_COOKIE_DOMAIN = config.session_cookie_domain,
|
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
|
return app
|
||||||
|
|
|
@ -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)
|
Loading…
Reference in New Issue