Refactor the auth component init a bit

This commit is contained in:
Tim Van Baak 2020-04-22 22:47:51 -07:00
parent 96db62fb35
commit d8a18854cb
2 changed files with 37 additions and 33 deletions

View File

@ -4,7 +4,7 @@ from flask import Flask, render_template
from flask_login import LoginManager from flask_login import LoginManager
from amanuensis.config import get, root 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.home import get_bp as get_home_bp
from amanuensis.server.helpers import register_custom_filters from amanuensis.server.helpers import register_custom_filters
from amanuensis.server.lexicon import get_bp as get_lex_bp from amanuensis.server.lexicon import get_bp as get_lex_bp
@ -29,7 +29,9 @@ login.login_view = 'auth.login'
login.anonymous_user = AnonymousUserModel login.anonymous_user = AnonymousUserModel
# Blueprint inits # 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) app.register_blueprint(auth_bp)
home_bp = get_home_bp() home_bp = get_home_bp()

View File

@ -1,22 +1,26 @@
import time import time
from flask import Blueprint, render_template, redirect, url_for, flash from flask import Blueprint, render_template, redirect, url_for, flash, current_app
from flask_login import login_user, logout_user, login_required from flask_login import login_user, logout_user, login_required, LoginManager
from amanuensis.config import logger, json_rw from amanuensis.config import logger, json_rw
from amanuensis.server.forms import LoginForm from amanuensis.server.forms import LoginForm
from amanuensis.user import UserModel from amanuensis.user import UserModel, AnonymousUserModel
def get_bp(login_manager): # TODO refactor login init into a func that takes a root cdc
"""Create a blueprint for the auth functions"""
bp = Blueprint('auth', __name__, url_prefix='/auth')
@login_manager.user_loader login_manager = LoginManager()
def load_user(uid): login_manager.login_view = 'auth.login'
login_manager.anonymous_user = AnonymousUserModel
bp = Blueprint('auth', __name__, url_prefix='/auth')
@login_manager.user_loader
def load_user(uid):
return UserModel.by(uid=str(uid)) return UserModel.by(uid=str(uid))
@bp.route('/login/', methods=['GET', 'POST']) @bp.route('/login/', methods=['GET', 'POST'])
def login(): def login():
form = LoginForm() form = LoginForm()
if form.validate_on_submit(): if form.validate_on_submit():
username = form.username.data username = form.username.data
@ -32,10 +36,8 @@ def get_bp(login_manager):
flash("Login not recognized") flash("Login not recognized")
return render_template('auth/login.html', form=form) return render_template('auth/login.html', form=form)
@bp.route("/logout/", methods=['GET']) @bp.route("/logout/", methods=['GET'])
@login_required @login_required
def logout(): def logout():
logout_user() logout_user()
return redirect(url_for('home.home')) return redirect(url_for('home.home'))
return bp