login.js 1.49 KB
function LoginBoton(tc, validate, redirect) {
    button = document.createElement("button");
    button.value = "Enviar";
    button.setAttribute("onclick", "if (validate('"+validate+"') == true) redirect('"+redirect+"')");
    button.setAttribute("class","SendButton");
    button.innerText = "Enviar";
    tc.lastElementChild.children[0].appendChild(button);
}

function CreateErrorLogin(fc){
    error = document.createElement("div");
    error.id = "ErrorLogin";
    error.innerText = "Usuario o Contraseña incorrectos";
    error.className = "Error";
    fc.appendChild(error);
}

function Redireccionar(bool, redirect){ 
    if (bool == true) window.location.href = redirect;
    else document.getElementById('ErrorLogin').style.display = "block";
}

function validate(url){
    cookie = new CookieHandler;
    data = Leer(document.getElementById('TabsContainer'))
    if (data == false) return false;
    let obj = {
        "url" : "/validate",
        "contentType" : "application/json",
        "async" : false,
        "data" : {
            "url" : url,
            "user" : data['Usuario'],
            "password" : data['Contraseña'],
        },
        "success" : (response) => {
            r = JSON.parse(response);
            if (r.success == true) cookie.add("login", r.cookie, 8 * 60 * 60 * 1000); // 8 hours
        }
    }
    ajax = new Ajax(obj);
    ajax.post();
    return true;
}

function redirect(url){
    cookie = new CookieHandler;
    window.location.href = url + "/" + cookie.get("login");
}