PageRenderTime 132ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/Resources/lib/login.js

https://bitbucket.org/jdenton/stuffsafe-mobile-ipad
JavaScript | 96 lines | 84 code | 9 blank | 3 comment | 14 complexity | 9c4a0a0823f38cbef9b7b754e5a77d34 MD5 | raw file
Possible License(s): Apache-2.0
  1. Login = {}
  2. Login.tryLogin = function(userid,password) {
  3. tryLoginAtServer(userid,password);
  4. }
  5. Login.tryAutoLogin = function() {
  6. var db = Titanium.Database.open('ss_ipad');
  7. var rows = db.execute("SELECT * FROM ss_login LIMIT 1");
  8. while (rows.isValidRow()) {
  9. var userid = rows.fieldByName('Email');
  10. var password = rows.fieldByName('Password');
  11. var authKey = rows.fieldByName('AuthKey');
  12. var userType = rows.fieldByName('UserType');
  13. var permissions = rows.fieldByName('Permissions');
  14. rows.next();
  15. }
  16. db.close();
  17. /*
  18. * Try logging in if we have a userid
  19. */
  20. if (userid != '' && userid != null) {
  21. tryLoginAtServer(userid,password,true);
  22. } else {
  23. Ti.include('ui/Login.js');
  24. LoginView.init();
  25. AppInit.loginWindow.add(LoginView.view);
  26. AppInit.loginWindow.open();
  27. }
  28. }
  29. function tryLoginAtServer(userid,password,autoLogin) {
  30. globals.ai._show({message: 'Logging in...'});
  31. var ajax = Titanium.Network.createHTTPClient();
  32. ajax.onerror = function(e) {
  33. alert('Error');
  34. };
  35. ajax.onload = function() {
  36. var data = this.responseText;
  37. var jdata = JSON.parse(data);
  38. if (jdata.Error == '1') {
  39. alert('Sorry! We could not find your account.');
  40. } else {
  41. globals.AUTH_KEY = jdata.AuthKey;
  42. globals.USER_TYPE = jdata.UserType;
  43. globals.PERMISSIONS = jdata.Permissions;
  44. jdata.Email = userid;
  45. jdata.Password = password;
  46. if (autoLogin != true) {
  47. saveCredentials(jdata);
  48. }
  49. AppInit.spinUpApp();
  50. }
  51. };
  52. ajax.open('POST', globals.SITE_URL+'login/tryLogin/1');
  53. ajax.send({
  54. 'userid' : userid,
  55. 'password' : password
  56. });
  57. }
  58. function saveCredentials(jdata) {
  59. var email = jdata.Email;
  60. var password = jdata.Password;
  61. var authKey = jdata.AuthKey;
  62. var userType = jdata.UserType;
  63. var permissions = jdata.Permissions;
  64. var db = Titanium.Database.open('ss_ipad');
  65. db.execute('DELETE FROM ss_login');
  66. db.execute('INSERT INTO ss_login (Email, Password, AuthKey, Permissions, UserType) values (?,?,?,?,?)', email, password, authKey, permissions, userType);
  67. db.close();
  68. }
  69. Login.findPassword = function(email) {
  70. var ajax = Titanium.Network.createHTTPClient();
  71. ajax.onerror = function(e) {
  72. alert('Error');
  73. };
  74. ajax.onload = function(email) {
  75. var data = this.responseText;
  76. var jdata = JSON.parse(data);
  77. if (jdata.Error == '1') {
  78. alert('Sorry! We could not find your account.');
  79. } else {
  80. alert('Your new password has been emailed to you.');
  81. }
  82. };
  83. ajax.open('POST', globals.SITE_URL+'login/findPassword/1');
  84. ajax.send({
  85. 'email': email
  86. });
  87. }