A self hosted chat application with end-to-end encrypted messaging.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.4 KiB

  1. var secondClick = false;
  2. function storeCredentials() {
  3. let usernameInput = document.getElementById('username');
  4. let passwordInput = document.getElementById('password');
  5. let jqxhr = $.ajax({
  6. type: 'GET',
  7. url: `/api/chat/get/token`,
  8. headers: {
  9. "Authorization": "Basic " + btoa(usernameInput.value + ":" + passwordInput.value)
  10. },
  11. });
  12. //this section is executed when the server responds with no error
  13. jqxhr.done(function() {
  14. let authToken = jqxhr.getResponseHeader('X-AUTH-TOKEN');
  15. localStorage.setItem('authToken', authToken);
  16. authToken = localStorage.getItem('authToken')
  17. console.log("getting header " + authToken);
  18. // secondClick = true;
  19. $('#loginForm').submit();
  20. });
  21. //this section is executed when the server responds with error
  22. jqxhr.fail(function() {
  23. log.error('Error retrieving auth token');
  24. alertify.error('Error retrieving auth token. Please log in again')
  25. secondClick = false;
  26. // setTimeout(() => location.reload(), 2000)
  27. })
  28. localStorage.setItem('username', usernameInput.value);
  29. }
  30. $('#loginForm').on('submit', function(e) {
  31. if (!secondClick) {
  32. secondClick = true;
  33. e.preventDefault();
  34. storeCredentials(secondClick);
  35. }
  36. // else {
  37. // secondClick = false;
  38. // }
  39. })