From d8a18854cb16d261b3d6b7319a8ff2a3b02a55c7 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Wed, 22 Apr 2020 22:47:51 -0700 Subject: [PATCH] Refactor the auth component init a bit --- amanuensis/server/__init__.py | 6 ++-- amanuensis/server/auth.py | 64 ++++++++++++++++++----------------- 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/amanuensis/server/__init__.py b/amanuensis/server/__init__.py index a124a38..a80385b 100644 --- a/amanuensis/server/__init__.py +++ b/amanuensis/server/__init__.py @@ -4,7 +4,7 @@ from flask import Flask, render_template from flask_login import LoginManager from amanuensis.config import get, root -from amanuensis.server.auth import get_bp as get_auth_bp +# from amanuensis.server.auth import get_bp as get_auth_bp from amanuensis.server.home import get_bp as get_home_bp from amanuensis.server.helpers import register_custom_filters from amanuensis.server.lexicon import get_bp as get_lex_bp @@ -29,7 +29,9 @@ login.login_view = 'auth.login' login.anonymous_user = AnonymousUserModel # Blueprint inits -auth_bp = get_auth_bp(login) +from amanuensis.server.auth import bp as auth_bp +from amanuensis.server.auth import login_manager as login_manager +login_manager.init_app(app) app.register_blueprint(auth_bp) home_bp = get_home_bp() diff --git a/amanuensis/server/auth.py b/amanuensis/server/auth.py index 3d378f2..2f1d00d 100644 --- a/amanuensis/server/auth.py +++ b/amanuensis/server/auth.py @@ -1,41 +1,43 @@ import time -from flask import Blueprint, render_template, redirect, url_for, flash -from flask_login import login_user, logout_user, login_required +from flask import Blueprint, render_template, redirect, url_for, flash, current_app +from flask_login import login_user, logout_user, login_required, LoginManager from amanuensis.config import logger, json_rw from amanuensis.server.forms import LoginForm -from amanuensis.user import UserModel +from amanuensis.user import UserModel, AnonymousUserModel -def get_bp(login_manager): - """Create a blueprint for the auth functions""" - bp = Blueprint('auth', __name__, url_prefix='/auth') +# TODO refactor login init into a func that takes a root cdc - @login_manager.user_loader - def load_user(uid): - return UserModel.by(uid=str(uid)) +login_manager = LoginManager() +login_manager.login_view = 'auth.login' +login_manager.anonymous_user = AnonymousUserModel - @bp.route('/login/', methods=['GET', 'POST']) - def login(): - form = LoginForm() - if form.validate_on_submit(): - username = form.username.data - u = UserModel.by(name=username) - if u is not None and u.check_password(form.password.data): - remember_me = form.remember.data - login_user(u, remember=remember_me) - with json_rw(u.config_path) as cfg: - cfg.last_login = int(time.time()) - logger.info("Logged in user '{}' ({})".format( - u.username, u.uid)) - return redirect(url_for('home.home')) - flash("Login not recognized") - return render_template('auth/login.html', form=form) +bp = Blueprint('auth', __name__, url_prefix='/auth') - @bp.route("/logout/", methods=['GET']) - @login_required - def logout(): - logout_user() - return redirect(url_for('home.home')) +@login_manager.user_loader +def load_user(uid): + return UserModel.by(uid=str(uid)) - return bp +@bp.route('/login/', methods=['GET', 'POST']) +def login(): + form = LoginForm() + if form.validate_on_submit(): + username = form.username.data + u = UserModel.by(name=username) + if u is not None and u.check_password(form.password.data): + remember_me = form.remember.data + login_user(u, remember=remember_me) + with json_rw(u.config_path) as cfg: + cfg.last_login = int(time.time()) + logger.info("Logged in user '{}' ({})".format( + u.username, u.uid)) + return redirect(url_for('home.home')) + flash("Login not recognized") + return render_template('auth/login.html', form=form) + +@bp.route("/logout/", methods=['GET']) +@login_required +def logout(): + logout_user() + return redirect(url_for('home.home'))