Skip to content
Snippets Groups Projects
auth.py 1.12 KiB
Newer Older
Alex's avatar
Alex committed
from flask import Blueprint, render_template, request, redirect, flash
from imperial_ldap.auth import ldap_login, ldap_logout
from config import url_for2
from imperial_doc_materials import materials_login

auth_blueprint = Blueprint("auth", __name__, url_prefix="/auth")


@auth_blueprint.route("/login", methods=["GET", "POST"])
def login():
    if request.method == "GET":
        return render_template("login.html") 
    
    # Handle post request
    username = request.form.get("username")
    password = request.form.get("password")
    if username and password:

        user = ldap_login(username, password)
        if user:
            # We're logged in here, so now we also login into the Material API
            materials_login(username, password)
            return redirect(url_for2("home.dashboard"))

        else:
            # bad credentials
            flash("Invalid login credentials.")
            return redirect(url_for2("auth.login"))

    return "Please provide a username and password"


@auth_blueprint.route("/logout", methods=["GET"])
def logout():
    ldap_logout()
    return redirect(url_for2("home.index"))