authorization.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* exported beginAuthorization, authorizeGoogleSilently, popupGoogleAuthorization, authorizeBilling, handleAuthResult, handleReAuthResult, billingAuthorizationFailed */
  2. /* exported logout */
  3. var token_google;
  4. var token_api;
  5. function toLogin()
  6. {
  7. showPage('login');
  8. var email = $('#email').val();
  9. if(email || email.length > 0)
  10. $('#password').focus();
  11. else
  12. $('#email').focus();
  13. }
  14. function login(e)
  15. {
  16. e.preventDefault();
  17. var email = $('#email').val();
  18. var password = $('#password').val();
  19. //clear the error message
  20. $('#login-error-message').html(' ');
  21. //set processing
  22. $('#form-login')
  23. .removeClass('error')
  24. .addClass('processing');
  25. //attempt to validate
  26. api['auth'].create('local', { 'email': email, 'password': password }).done(function(creds)
  27. {
  28. if(!creds)
  29. {
  30. console.log('no credentials');
  31. return;
  32. }
  33. credentials = creds;
  34. token = credentials['token'];
  35. //store credentials locally
  36. localStorage['credentials'] = JSON.stringify(credentials);
  37. $('.user_name_first').html(creds['user']['name_first']);
  38. //store inside API
  39. api['opts']['token'] = credentials['token'];
  40. //make sure app page is visible
  41. $('#app').show();
  42. //reload router
  43. router.reload();
  44. //fade away the login screen
  45. $('#form-login').fadeOut();
  46. })
  47. .fail(function(xhr)
  48. {
  49. var msg = xhr.responseText;
  50. $('#login-error-message').html(msg);
  51. $('#form-login').addClass('error');
  52. $('#password').val('').focus();
  53. })
  54. .always(function()
  55. {
  56. $('#form-login').removeClass('processing');
  57. });
  58. return false;
  59. }
  60. function logout(e)
  61. {
  62. console.log('logout');
  63. //clear the local session
  64. localStorage['credentials'] = null;
  65. //fade in the login screen
  66. showPage('login');
  67. $('.user_name_first').html('[User]');
  68. if(table_properties)
  69. table_properties.clear().draw();
  70. if(table_dashboard_properties)
  71. table_dashboard_properties.clear().draw();
  72. //TODO: clear data lists
  73. e.preventDefault();
  74. return false;
  75. }