Chatto/chatto/src/main/resources/static/js/login.js

39 lines
1.3 KiB
JavaScript
Raw Normal View History

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',
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) {
if (!secondClick) {
2019-11-29 19:13:50 +00:00
secondClick = true;
e.preventDefault();
storeCredentials();
}
// else {
// secondClick = false;
// }
})