spacedeck_users.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. SpacedeckUsers
  3. This module contains functions dealing with Users and Authentication.
  4. */
  5. SpacedeckUsers = {
  6. data: {
  7. user_forms_email: "",
  8. user_forms_name: "",
  9. invitation_token: null,
  10. login_email: "",
  11. login_password: "",
  12. signup_password: "",
  13. signup_password_confirmation: "",
  14. account_remove_error: null,
  15. loading_user: false,
  16. password_reset_confirm_error: "",
  17. password_reset_error: "",
  18. },
  19. methods:{
  20. load_user: function(on_success, on_error) {
  21. this.loading_user = true;
  22. load_current_user(function(user) {
  23. this.user = user;
  24. this.loading_user = false;
  25. this.logged_in = true;
  26. if (on_success) {
  27. on_success(user);
  28. }
  29. // see spacedeck_account.js
  30. load_importables(this.user, function(files) {
  31. this.importables = files;
  32. }.bind(this));
  33. }.bind(this), function() {
  34. // error
  35. this.loading_user = false;
  36. this.logout();
  37. if (on_error) {
  38. on_error();
  39. }
  40. }.bind(this));
  41. },
  42. finalize_login: function(session_token, on_success) {
  43. this.load_user(function(user) {
  44. if (this.invitation_token) {
  45. accept_invitation(this.invitation_token, function(memberships){
  46. this.redirect_to("/spaces/"+memberships.space_id);
  47. }.bind(this), function(xhr){
  48. console.error(xhr);
  49. alert("Could not accept invitation. Maybe it was already accepted?");
  50. this.redirect_to("/spaces");
  51. }.bind(this));
  52. } else {
  53. if (on_success) {
  54. on_success(this.user);
  55. } else {
  56. if (get_query_param("space_id") && get_query_param("space_id").length==24) {
  57. this.redirect_to("/spaces/"+get_query_param("space_id"));
  58. } else {
  59. this.redirect_to("/spaces", function() {});
  60. }
  61. }
  62. }
  63. }.bind(this));
  64. },
  65. login_with_token: function(token) {
  66. create_session_for_oauthtoken(token, function(session) {
  67. this.session = session;
  68. this.finalize_login(session.token);
  69. }.bind(this), function(xhr){
  70. // FIXME: handle error
  71. }.bind(this));
  72. },
  73. login_submit: function(email, password, $event, on_success) {
  74. this.loading_user = true;
  75. this.login_error = null;
  76. if ($event) {
  77. $event.preventDefault();
  78. $event.stopPropagation();
  79. }
  80. create_session(email, password, function(session) {
  81. console.log("session: ", session);
  82. this.loading_user = false;
  83. this.session = session;
  84. this.finalize_login(session.token, on_success);
  85. }.bind(this), function(req) {
  86. this.loading_user = false;
  87. var msg = "";
  88. if (req.status>=403) {
  89. var msg = "error_unknown_email";
  90. } else {
  91. try {
  92. var msg = "error_"+(JSON.parse(req.responseText).error);
  93. } catch (e) {
  94. var msg = (req.responseText||"Unknown Error.").replace(/,/g," ");
  95. }
  96. }
  97. this.login_error = __(msg);
  98. }.bind(this));
  99. },
  100. login_submit_modal: function(email, password) {
  101. this.login_submit(email, password, null, function() {
  102. location.reload();
  103. });
  104. },
  105. signup_guest: function(on_success) {
  106. },
  107. signup_submit: function($event, name, email, password, password_confirmation, on_success) {
  108. this.creating_user = true;
  109. this.signup_error = null;
  110. if (("localStorage" in window) && localStorage) {
  111. localStorage["sd_api_token"] = null;
  112. }
  113. api_token = null;
  114. if ($event) {
  115. $event.preventDefault();
  116. $event.stopPropagation();
  117. }
  118. create_user(name, email, password, password_confirmation, function(session) {
  119. this.creating_user = false;
  120. this.login_submit(email, password, null, on_success);
  121. }.bind(this), function(req) {
  122. this.creating_user = false;
  123. try {
  124. var msg = "error_"+(JSON.parse(req.responseText).error);
  125. } catch (e) {
  126. var msg = (req.responseText||"Unknown Error.").replace(/,/g," ");
  127. }
  128. var msg = __(msg);
  129. this.signup_error = msg;
  130. }.bind(this));
  131. },
  132. signup_submit_modal: function($event, name, email, password, password_confirmation) {
  133. this.signup_submit($event, name, email, password, password_confirmation, function() {
  134. alert("Success.");
  135. location.reload();
  136. });
  137. },
  138. password_reset_submit: function(evt, email) {
  139. if (evt) {
  140. evt.preventDefault();
  141. evt.stopPropagation();
  142. }
  143. this.password_reset_error = null;
  144. this.password_reset_send = false;
  145. if (email === undefined || email.length < 3) {
  146. this.password_reset_error = "This is not a valid email address";
  147. return;
  148. }
  149. create_password_reset(email, function(parsed,req) {
  150. if(req.status==201) {
  151. this.password_reset_send = true;
  152. }
  153. }.bind(this), function(req) {
  154. console.log(req.status);
  155. if (req.status==404) {
  156. var msg = "error_unknown_email";
  157. } else {
  158. try {
  159. var msg = "error_"+(JSON.parse(req.responseText).error);
  160. } catch (e) {
  161. var msg = (req.responseText||"Unknown Error.").replace(/,/g," ");
  162. }
  163. }
  164. this.password_reset_error = __(msg);
  165. }.bind(this));
  166. },
  167. password_reset_confirm: function(evt, password, password_confirmation) {
  168. if (evt) {
  169. evt.preventDefault();
  170. evt.stopPropagation();
  171. }
  172. this.password_reset_confirm_error = null;
  173. this.password_reset_send = false;
  174. if(password != password_confirmation) {
  175. this.password_reset_confirm_error = "Passwords do not match.";
  176. return;
  177. }
  178. if(password.length < 5) {
  179. this.password_reset_confirm_error = "Password too short (must have at least 5 characters).";
  180. return;
  181. }
  182. confirm_password_reset(password, this.reset_token, function(parsed,req) {
  183. if(req.status==201){
  184. this.active_view = "login";
  185. }
  186. }.bind(this), function(req) {
  187. if (req.status==404) {
  188. var msg = "user not found";
  189. } else {
  190. var msg = "error: " + req.statusText;
  191. }
  192. this.password_reset_confirm_error = msg;
  193. }.bind(this));
  194. },
  195. logout: function() {
  196. this.active_view="login";
  197. this.logged_in = false;
  198. delete_session(function() {
  199. this.active_space = {advanced:{}};
  200. this.active_space_loaded = false;
  201. this.active_sidebar_item = "none";
  202. this.sidebar_state = "closed";
  203. this.loading_user = false;
  204. api_token = null;
  205. this.user = {};
  206. this.active_content_type = "login";
  207. this.redirect_to("/");
  208. }.bind(this));
  209. },
  210. send_feedback: function(text) {
  211. if (text.length>0) {
  212. create_feedback(this.user, text, function(xhr) {
  213. alert(__("feedback_sent"));
  214. this.close_modal()
  215. }.bind(this), function(xhr) {
  216. console.error(xhr);
  217. });
  218. }
  219. },
  220. remove_account: function(password, reason) {
  221. this.account_remove_error = null;
  222. if (reason && reason.length && (reason.length > 1)) {
  223. create_feedback(this.user, reason, function(xhr) {
  224. console.log("feedback sent");
  225. }, function(xhr){});
  226. }
  227. if (!password) {
  228. this.account_remove_error = "Password not correct";
  229. return;
  230. }
  231. delete_user(this.user, password, function(xhr) {
  232. alert("Sorry to see you go. Goodbye!");
  233. this.logout();
  234. }.bind(this), function(xhr) {
  235. this.account_remove_error = "Password not correct ("+xhr.status+")";
  236. }.bind(this));
  237. },
  238. user_avatar_image: function(user) {
  239. return user.avatar_thumb_uri;
  240. },
  241. user_initials: function(user) {
  242. var parts = (user?(user.nickname||user.email):"anonymous").replace(/[^a-zA-Z]/g,' ').replace(/ +/g,' ').split(' ');
  243. if (parts.length>1) {
  244. return parts[0][0]+parts[1][0];
  245. }
  246. return parts[0].substring(0,2);
  247. },
  248. has_avatar_image: function(user) {
  249. return !!(user && user.avatar_thumb_uri && user.avatar_thumb_uri.length>0);
  250. },
  251. is_pro: function(user) {
  252. return true;
  253. },
  254. }
  255. }