2019-11-23 23:37:01 +00:00
|
|
|
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);
|
2020-02-29 20:18:37 +00:00
|
|
|
let box = document.getElementById("login_msg");
|
|
|
|
if(json.message && json.message == "msg.auth.login.successful") {
|
|
|
|
if(window.location.pathname == "/authenticate" && getParameterByName("appId")) window.location.href= "/authenticate";
|
|
|
|
else window.location.href= "/";
|
|
|
|
|
|
|
|
box.classList.remove("uk-hidden");
|
|
|
|
box.classList.add("uk-alert-success");
|
|
|
|
box.classList.getElementsByTagName("p")[0].innerHTML = "Logged in. You will be redirected";
|
|
|
|
} else if(json.message && json.message == "msg.auth.login.failed") {
|
|
|
|
box.classList.remove("uk-hidden");
|
|
|
|
box.classList.add("uk-alert-danger");
|
|
|
|
box.getElementsByTagName("p")[0].innerHTML = "Login failed.<br> Username or Password is wrong.";
|
|
|
|
}
|
2019-11-23 23:37:01 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
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")) {
|
|
|
|
console.log("logout");
|
|
|
|
logout();
|
|
|
|
}
|
|
|
|
}, 100);
|
2020-02-29 20:18:37 +00:00
|
|
|
|
|
|
|
// @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, ' '));
|
|
|
|
}
|