-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
54 lines (45 loc) · 1.72 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';
var buttonLogin = document.getElementById("buttonLogin_js");
var loginErrorText = document.querySelector(".login-error");
var request = new XMLHttpRequest();
buttonLogin.addEventListener("click", login);
function getUserAccountInfo() {
var username = document.getElementById("username_js").value;
var password = document.getElementById("password_js").value;
var payload = {
username: username ,
password: password ,
type:"normal"
};
return payload;
}
function login() {
request.open('POST', 'https://api.taiga.io/api/v1/auth', true); // use true to make the request async
request.setRequestHeader("Content-Type", "application/json");
request.setRequestHeader("Accept", "application/json");
request.onload = function() {
var data = JSON.parse(request.responseText);
if (request.status >= 200 && request.status < 400) {
var value = data.auth_token;
console.log("Funciona!");
var userData = data;
sessionStorage.setItem("token", JSON.stringify(value));
sessionStorage.setItem("user", JSON.stringify(userData));
window.location.href = "dashboard.html" ;
} else{
if (data._error_type === "taiga.base.exceptions.WrongArguments") {
loginErrorText.innerHTML = "Username or password does not matches user." ;
loginErrorText.style.backgroundColor = '#ef0707';
loginErrorText.style.color = '#ffffff';
loginErrorText.style.fontSize = '1.2em';
} else {
console.log("La respuesta del servidor ha devuelto un error");
}
}
};
request.onerror = function() {
console.log("Error al tratar de conectarse con el servidor");
};
var userInfo = getUserAccountInfo();
request.send(JSON.stringify(userInfo));
}