auth.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var page = {};
  2. page.do = function(dest){
  3. var user = document.getElementById('user').value;
  4. var pass = document.getElementById('pass').value;
  5. if(user === undefined || user === null || user === '')
  6. return swal('Error', 'You need to specify a username', 'error');
  7. if(pass === undefined || pass === null || pass === '')
  8. return swal('Error', 'You need to specify a username', 'error');
  9. axios.post('/api/' + dest, {
  10. username: user,
  11. password: pass
  12. })
  13. .then(function (response) {
  14. if(response.data.success === false)
  15. return swal('Error', response.data.description, 'error');
  16. localStorage.token = response.data.token;
  17. window.location = '/panel';
  18. })
  19. .catch(function (error) {
  20. return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error');
  21. console.log(error);
  22. });
  23. }
  24. page.verify = function(){
  25. page.token = localStorage.token;
  26. if(page.token === undefined) return;
  27. axios.post('/api/tokens/verify', {
  28. token: page.token
  29. })
  30. .then(function (response) {
  31. if(response.data.success === false)
  32. return swal('Error', response.data.description, 'error');
  33. window.location = '/panel';
  34. })
  35. .catch(function (error) {
  36. return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error');
  37. console.log(error);
  38. });
  39. }
  40. window.onload = function () {
  41. page.verify();
  42. }