haldis/app/login.py

33 lines
779 B
Python
Raw Normal View History

2019-09-08 01:58:21 +02:00
from flask import Blueprint, abort, redirect, session, url_for
from flask_login import current_user, logout_user
2019-09-08 01:58:21 +02:00
from werkzeug.wrappers import Response
2015-03-31 20:15:22 +02:00
from models import User
from zeus import zeus_login
2019-09-05 03:33:29 +02:00
auth_bp = Blueprint("auth_bp", __name__)
2015-03-31 20:15:22 +02:00
2019-09-08 01:58:21 +02:00
def init_login(app) -> None:
@app.login_manager.user_loader
2019-09-08 01:58:21 +02:00
def load_user(userid) -> User:
return User.query.filter_by(id=userid).first()
2015-03-31 20:15:22 +02:00
2019-09-05 03:33:29 +02:00
@auth_bp.route("/login")
2015-03-31 20:15:22 +02:00
def login():
return zeus_login()
2019-09-05 03:33:29 +02:00
@auth_bp.route("/logout")
2019-09-08 01:58:21 +02:00
def logout() -> Response:
2019-09-05 03:33:29 +02:00
if "zeus_token" in session:
session.pop("zeus_token", None)
2015-03-31 20:15:22 +02:00
logout_user()
2019-09-05 03:33:29 +02:00
return redirect(url_for("general_bp.home"))
2015-03-31 20:15:22 +02:00
2019-09-08 01:58:21 +02:00
def before_request() -> None:
2015-03-31 20:15:22 +02:00
if current_user.is_anonymous() or not current_user.is_allowed():
abort(401)