haldis/app/auth/login.py

33 lines
946 B
Python
Raw Normal View History

2022-04-20 01:27:52 +02:00
"""Script for everything related to logging in and out"""
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
2015-03-31 20:15:22 +02:00
from models import User
2022-04-19 22:03:00 +02:00
from werkzeug.wrappers import Response
2015-03-31 20:15:22 +02:00
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:
2022-04-20 01:27:52 +02:00
"""Initialize the login"""
2019-09-10 15:17:35 +02:00
# pylint: disable=W0612
@app.login_manager.user_loader
2019-09-08 01:58:21 +02:00
def load_user(userid) -> User:
2022-04-20 01:27:52 +02:00
"""Load the 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("/logout")
2019-09-08 01:58:21 +02:00
def logout() -> Response:
2022-04-20 01:27:52 +02:00
"""Function to handle a user trying to log out"""
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:
2022-04-20 01:27:52 +02:00
"""Function for what has to be done before a request"""
2015-03-31 20:15:22 +02:00
if current_user.is_anonymous() or not current_user.is_allowed():
abort(401)