2019-11-21 05:40:21 +00:00
|
|
|
function storeCredentials() {
|
|
|
|
let usernameInput = document.getElementById('username');
|
|
|
|
let passwordInput = document.getElementById('password');
|
|
|
|
var jqxhr = $.ajax({
|
|
|
|
type: 'GET',
|
2019-12-07 06:01:42 +00:00
|
|
|
url: `/api/chat/get/token`,
|
2019-11-21 05:40:21 +00:00
|
|
|
headers: {
|
|
|
|
"Authorization": "Basic " + btoa(usernameInput.value + ":" + passwordInput.value)
|
|
|
|
},
|
|
|
|
});
|
|
|
|
//this section is executed when the server responds with no error
|
|
|
|
jqxhr.done(function() {
|
|
|
|
let authToken = jqxhr.getResponseHeader('X-AUTH-TOKEN');
|
|
|
|
localStorage.setItem('authToken', authToken);
|
2019-11-29 19:13:50 +00:00
|
|
|
authToken = localStorage.getItem('authToken')
|
2019-11-21 05:40:21 +00:00
|
|
|
console.log("getting header " + authToken);
|
2019-11-29 19:13:50 +00:00
|
|
|
secondClick = true;
|
|
|
|
$('#loginForm').submit();
|
|
|
|
|
2019-11-21 05:40:21 +00:00
|
|
|
});
|
|
|
|
//this section is executed when the server responds with error
|
|
|
|
jqxhr.fail(function() {
|
2019-11-29 19:13:50 +00:00
|
|
|
log.error('Error retrieving auth token');
|
|
|
|
alertify.error('Error retrieving auth token. Please log in again')
|
2019-11-21 05:40:21 +00:00
|
|
|
})
|
2019-11-29 19:13:50 +00:00
|
|
|
localStorage.setItem('username', usernameInput.value);
|
2019-11-21 05:40:21 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 19:13:50 +00:00
|
|
|
let secondClick = false;
|
|
|
|
$('#loginForm').on('submit', function(e) {
|
2019-12-07 06:01:42 +00:00
|
|
|
if (!secondClick) {
|
2019-11-29 19:13:50 +00:00
|
|
|
secondClick = true;
|
|
|
|
e.preventDefault();
|
|
|
|
storeCredentials();
|
|
|
|
}
|
|
|
|
// else {
|
|
|
|
// secondClick = false;
|
|
|
|
// }
|
2019-12-07 06:01:42 +00:00
|
|
|
})
|