PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/public/javascripts/auth.js

https://github.com/clint-tseng/odkbuild
JavaScript | 257 lines | 225 code | 21 blank | 11 comment | 8 complexity | 6a13934fe0bc5c8ba0b0e16dae3bdf9a MD5 | raw file
  1. /**
  2. * auth.js - keeper of the grounds
  3. * Basic bits to verify user login and present
  4. * appropriate UI when necessary.
  5. */
  6. var authNS = odkmaker.namespace.load('odkmaker.auth');
  7. ;(function($)
  8. {
  9. authNS.currentUser = null;
  10. var signinSuccessful = function(response, status)
  11. {
  12. $('.accountStatus')
  13. .empty()
  14. .append('Signed in as <a href="#accountDialog" rel="modal">' +
  15. authNS.currentUser.display_name + '</a>. <a href="#signout" ' +
  16. 'class="signoutLink">Sign out</a>.')
  17. .fadeIn('slow');
  18. $('.signinDialog')
  19. .find(':input')
  20. .val('')
  21. .end()
  22. .jqmHide();
  23. };
  24. var noAuthMessage = function()
  25. {
  26. $('.accountStatus')
  27. .empty()
  28. .append('Not signed in. <a href="#signinDialog" rel="modal">' +
  29. 'Sign in now</a>.')
  30. .fadeIn('slow');
  31. };
  32. authNS.verify = function(callback)
  33. {
  34. // Get current user status from server
  35. $.ajax({
  36. url: '/user',
  37. dataType: 'json',
  38. type: 'GET',
  39. complete: function()
  40. {
  41. setTimeout(function()
  42. {
  43. $('.loadingScreen')
  44. .fadeOut('normal', function()
  45. {
  46. $(this).remove();
  47. });
  48. $('.preloadImages').remove();
  49. }, 200); // give a bit of extra time in case the load was instantaneous
  50. },
  51. success: function(response, status)
  52. {
  53. authNS.currentUser = response;
  54. (callback || signinSuccessful)(response, status);
  55. },
  56. error: function(request, status, error)
  57. {
  58. authNS.currentUser = null;
  59. noAuthMessage();
  60. $('.signinDialog').jqmShow();
  61. }
  62. });
  63. };
  64. $(function()
  65. {
  66. // Signin dialog events
  67. $('.signinDialog .toggleSignupLink').click(function(event)
  68. {
  69. event.preventDefault();
  70. // TODO: this code sucks.
  71. $('.signinDialog .signup_section, p:has(.togglePasswordLink)').slideToggle();
  72. $('.signinDialog .signinLink, .signinDialog .signupLink').toggleClass('hide');
  73. if ($('.modalButton.signinLink').hasClass('hide'))
  74. {
  75. $(this).text('Never mind, I have an account.');
  76. $('.signinDialog h3').text('Sign up');
  77. $('.signinDialog .emailHint').hide();
  78. }
  79. else
  80. {
  81. $(this).text('Don\'t yet have an account?');
  82. $('.signinDialog h3').text('Sign in');
  83. $('.signinDialog .emailHint').show();
  84. }
  85. });
  86. $('.signinDialog .togglePasswordLink').click(function(event)
  87. {
  88. event.preventDefault();
  89. // TODO: this code still sucks.
  90. $('.signinDialog .signin_section, p:has(.toggleSignupLink)').slideToggle();
  91. $('.signinDialog .signinLink, .signinDialog .passwordLink').toggleClass('hide');
  92. if ($('.modalButton.signinLink').hasClass('hide'))
  93. {
  94. $(this).text('Never mind, I remembered it.');
  95. $('.signinDialog h3').text('Reset password');
  96. }
  97. else
  98. {
  99. $(this).text('Forgot your password?');
  100. $('.signinDialog h3').text('Sign in');
  101. }
  102. });
  103. $('.signinDialog .signinLink').click(function(event)
  104. {
  105. event.preventDefault();
  106. $('.signinDialog .errorMessage').slideUp();
  107. $.ajax({
  108. url: '/login',
  109. dataType: 'json',
  110. type: 'POST',
  111. data: $('.signinDialog form').find(':input:visible'),
  112. success: function(response, status)
  113. {
  114. authNS.currentUser = response;
  115. signinSuccessful(response, status);
  116. },
  117. error: function(request, status, error)
  118. {
  119. var message = 'Could not log you in with those credentials. Please try again.';
  120. if (request.status == 500)
  121. message = 'Something has gone wrong. Please try again in a bit, and report the issue if it persists.';
  122. $('.signinDialog .errorMessage')
  123. .empty()
  124. .append('<p>' + message + '</p>')
  125. .slideDown();
  126. }
  127. });
  128. });
  129. $('.signinDialog .passwordLink').click(function(event)
  130. {
  131. event.preventDefault();
  132. $('.signinDialog .errorMessage').slideUp();
  133. $.ajax({
  134. url: '/reset_password',
  135. dataType: 'json',
  136. type: 'POST',
  137. data: { username: $('.signinDialog form #signin_username').val() },
  138. success: function(response, status)
  139. {
  140. $('.signinDialog')
  141. .find(':input')
  142. .val('')
  143. .end()
  144. .jqmHide();
  145. $.toast('Your password has reset, and the new password has been emailed to you. Please check your inbox in a minute.');
  146. },
  147. error: function(request, status, error)
  148. {
  149. $('.signinDialog .errorMessage')
  150. .empty()
  151. .append('<p>We couldn\'t reset your password for some reason. Please check that your user name is correct, and try again in a bit.')
  152. .slideDown();
  153. }
  154. })
  155. });
  156. $('.signinDialog .signupLink').click(function(event)
  157. {
  158. event.preventDefault();
  159. $('.signinDialog .errorMessage').slideUp();
  160. if ($('.signinDialog form #signin_password').val() !==
  161. $('.signinDialog form #signup_password_confirm').val())
  162. {
  163. $('.signinDialog .errorMessage')
  164. .empty()
  165. .append('<p>The passwords you typed do not match.</p>')
  166. .slideDown();
  167. return;
  168. }
  169. $.ajax({
  170. url: '/users',
  171. dataType: 'json',
  172. type: 'POST',
  173. data: $('.signinDialog form').find(':input'),
  174. success: function(response, status)
  175. {
  176. authNS.currentUser = response;
  177. signinSuccessful(response, status);
  178. },
  179. error: function(request, status, error)
  180. {
  181. $('.signinDialog .errorMessage')
  182. .empty()
  183. .append('<p>Could not create an account with those credentials. Please try again.</p>')
  184. .slideDown();
  185. }
  186. });
  187. });
  188. // Sign out link
  189. $('.accountStatus').on('click', '.signoutLink', function(event)
  190. {
  191. event.preventDefault();
  192. $.ajax({
  193. url: '/logout',
  194. dataType: 'json',
  195. type: 'GET',
  196. success: function(response, status)
  197. {
  198. authNS.currentUser = null;
  199. odkmaker.data.currentForm = null;
  200. $('.accountStatus')
  201. .fadeOut('slow', noAuthMessage);
  202. $.toast('You have been successfully signed out. You can continue editing this form but you\'ll have to log in to save it.');
  203. },
  204. error: function(request, status, error)
  205. {
  206. $.toast('You could not be signed out at this time. Please try again in a moment.');
  207. }
  208. });
  209. });
  210. // Account modal events
  211. $('.accountDialog .updateAccountLink').click(function(event)
  212. {
  213. event.preventDefault();
  214. $('.accountDialog .errorMessage').slideUp();
  215. $.ajax({
  216. url: '/user/' + authNS.currentUser.username,
  217. dataType: 'json',
  218. type: 'PUT',
  219. data: $('.accountDialog form').find(':input'),
  220. success: function(response, status)
  221. {
  222. $('.accountDialog').jqmHide();
  223. $.toast('Your account information has been successfully updated.');
  224. },
  225. error: function(request, status, error)
  226. {
  227. $('.accountDialog .errorMessage')
  228. .empty()
  229. .append('<p>Could not update your account settings. Please try again.</p>')
  230. .slideDown();
  231. }
  232. });
  233. });
  234. authNS.verify();
  235. });
  236. })(jQuery);