178 lines
5.1 KiB
JavaScript
178 lines
5.1 KiB
JavaScript
|
|
// '/login', login user
|
|
function loginEvent() { login(); return false; } // btnEvent handling
|
|
function login() {
|
|
let user = document.getElementById("login_user").value;
|
|
let pass = document.getElementById("login_pass").value;
|
|
|
|
let data = {
|
|
"email": user,
|
|
"password": pass
|
|
};
|
|
|
|
let ajax = new XMLHttpRequest();
|
|
ajax.open("POST", "/api/login", true);
|
|
ajax.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
|
ajax.send(JSON.stringify(data));
|
|
ajax.onload = () => {
|
|
let json = JSON.parse(ajax.responseText);
|
|
let box = document.getElementById("login_msg");
|
|
|
|
box.classList.remove("uk-hidden");
|
|
box.classList.remove("uk-alert-success");
|
|
box.classList.remove("uk-alert-danger");
|
|
|
|
if(json.message && json.message == "msg.auth.login.successful") {
|
|
setTimeout(function () {
|
|
if(
|
|
window.location.pathname == "/authenticate" &&
|
|
getParameterByName("appId") &&
|
|
getParameterByName("redirectUrl")
|
|
) window.location.reload();
|
|
else window.location.href = "./";
|
|
}, 150);
|
|
|
|
box.classList.add("uk-alert-success");
|
|
box.getElementsByTagName("p")[0].innerHTML = "Logged in. You will be redirected";
|
|
} else if(json.message && json.message == "msg.auth.login.failed") {
|
|
box.classList.add("uk-alert-danger");
|
|
box.getElementsByTagName("p")[0].innerHTML = "Login failed.<br> Username or Password is wrong.";
|
|
}
|
|
};
|
|
};
|
|
|
|
// '/logout', logout user
|
|
function logout() {
|
|
let ajax = new XMLHttpRequest();
|
|
ajax.open("GET", "/api/logout", true);
|
|
ajax.setRequestHeader('Content-Type', 'charset=UTF-8');
|
|
ajax.send("");
|
|
ajax.onload = () => {
|
|
let json = JSON.parse(ajax.responseText);
|
|
if(json.message && json.message == "msg.auth.logout.successful") window.location.href = "./";
|
|
};
|
|
}
|
|
setTimeout(function () {
|
|
if(document.getElementById("listener_logout")) {
|
|
logout();
|
|
}
|
|
}, 100);
|
|
|
|
// '/authenticate', cancels Authentication
|
|
function cancelRequest() {
|
|
let ajax = new XMLHttpRequest();
|
|
ajax.open("GET", "/api/cancel", true);
|
|
ajax.setRequestHeader('Content-Type', 'charset=UTF-8');
|
|
ajax.send("");
|
|
ajax.onload = () => {
|
|
let json = JSON.parse(ajax.responseText);
|
|
if(json.message && json.message == "msg.request.operation.cancel.successful") window.location.href = "./";
|
|
};
|
|
}
|
|
|
|
// '/settings', shows MFA Secret on button click
|
|
function showMFASecret(e) {
|
|
let secret = document.getElementById(e.target.id).parentNode.parentNode.parentNode.getElementsByClassName("secret")[0];
|
|
if(!secret.classList.contains("uk-hidden")) {
|
|
secret.classList.add("uk-hidden");
|
|
} else {
|
|
secret.classList.remove("uk-hidden");
|
|
let secretData = secret.dataset.secret;
|
|
let options = secret.id.split(".");
|
|
// @TODO
|
|
switch (options[1]) {
|
|
case "HOTP":
|
|
break;
|
|
case "TOTP":
|
|
break;
|
|
case "WebAuthn":
|
|
break;
|
|
default:
|
|
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
// @TODO
|
|
// '/settings', removes MFA Layer
|
|
function removeMFA(e) {
|
|
return false;
|
|
}
|
|
|
|
// '/settings', saves Profile
|
|
function saveProfile(e) {
|
|
let email = document.getElementById("form-profile-email").value;
|
|
|
|
let data = {
|
|
"email": email
|
|
};
|
|
|
|
let ajax = new XMLHttpRequest();
|
|
ajax.open("POST", "/api/settings", true);
|
|
ajax.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
|
ajax.send(JSON.stringify(data));
|
|
ajax.onload = () => {
|
|
let json = JSON.parse(ajax.responseText);
|
|
let box = document.getElementById("profile_msg");
|
|
|
|
box.classList.remove("uk-hidden");
|
|
box.classList.remove("uk-alert-success");
|
|
box.classList.remove("uk-alert-danger");
|
|
|
|
if(json.message && json.message == "msg.settings.update.successful") {
|
|
|
|
box.classList.add("uk-alert-success");
|
|
box.getElementsByTagName("p")[0].innerHTML = "Update was successfully";
|
|
} else if(json.message) {
|
|
box.classList.add("uk-alert-danger");
|
|
box.getElementsByTagName("p")[0].innerHTML = "Update failed";
|
|
}
|
|
};
|
|
return false;
|
|
}
|
|
|
|
// '/settings', saves Password
|
|
function savePassword(e) {
|
|
let pass = document.getElementById("form-security-password").value;
|
|
let repass = document.getElementById("form-security-repassword").value;
|
|
|
|
let data = {
|
|
"password": pass,
|
|
"repassword": repass
|
|
};
|
|
|
|
let ajax = new XMLHttpRequest();
|
|
ajax.open("POST", "/api/settings", true);
|
|
ajax.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
|
ajax.send(JSON.stringify(data));
|
|
ajax.onload = () => {
|
|
let json = JSON.parse(ajax.responseText);
|
|
let box = document.getElementById("password_msg");
|
|
|
|
box.classList.remove("uk-hidden");
|
|
box.classList.remove("uk-alert-success");
|
|
box.classList.remove("uk-alert-danger");
|
|
|
|
if(json.message && json.message == "msg.settings.update.successful") {
|
|
|
|
box.classList.add("uk-alert-success");
|
|
box.getElementsByTagName("p")[0].innerHTML = "Update was successfully";
|
|
} else if(json.message) {
|
|
box.classList.add("uk-alert-danger");
|
|
box.getElementsByTagName("p")[0].innerHTML = "Update failed";
|
|
}
|
|
};
|
|
return false;
|
|
}
|
|
|
|
// @url: https://stackoverflow.com/a/901144
|
|
function getParameterByName(name) {
|
|
let url = window.location.href;
|
|
name = name.replace(/[\[\]]/g, '\\$&');
|
|
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
|
|
results = regex.exec(url);
|
|
if (!results) return null;
|
|
if (!results[2]) return '';
|
|
return decodeURIComponent(results[2].replace(/\+/g, ' '));
|
|
}
|