user.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import auth from "../../api/auth.js";
  2. import io from "../../io.js";
  3. import validation from "../../validation.js";
  4. const state = {};
  5. const getters = {};
  6. const actions = {};
  7. const mutations = {};
  8. const modules = {
  9. auth: {
  10. namespaced: true,
  11. state: {
  12. userIdMap: {},
  13. userIdRequested: {},
  14. waitingForUserIdCallbacks: {}
  15. },
  16. getters: {},
  17. actions: {
  18. /* eslint-disable-next-line no-unused-vars */
  19. register: ({ commit }, user) => {
  20. return new Promise((resolve, reject) => {
  21. const { username, email, password } = user;
  22. if (!email || !username || !password)
  23. return reject({
  24. status: "error",
  25. message: "Please fill in all fields"
  26. });
  27. if (!validation.isLength(email, 3, 254))
  28. return reject({
  29. status: "error",
  30. message:
  31. "Email must have between 3 and 254 characters."
  32. });
  33. if (
  34. email.indexOf("@") !== email.lastIndexOf("@") ||
  35. !validation.regex.emailSimple.test(email)
  36. )
  37. return reject({
  38. status: "error",
  39. message: "Invalid email format."
  40. });
  41. if (!validation.isLength(username, 2, 32))
  42. return reject({
  43. status: "error",
  44. message:
  45. "Username must have between 2 and 32 characters."
  46. });
  47. if (!validation.regex.azAZ09_.test(username))
  48. return reject({
  49. status: "error",
  50. message:
  51. "Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _."
  52. });
  53. if (!validation.isLength(password, 6, 200))
  54. return reject({
  55. status: "error",
  56. message:
  57. "Password must have between 6 and 200 characters."
  58. });
  59. if (!validation.regex.password.test(password))
  60. return reject({
  61. status: "error",
  62. message:
  63. "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character."
  64. });
  65. auth.register(user)
  66. .then(() => {
  67. return resolve({
  68. status: "success",
  69. message: "Account registered!"
  70. });
  71. })
  72. .catch(err => {
  73. return reject({
  74. status: "error",
  75. message: err.message
  76. });
  77. });
  78. });
  79. },
  80. /* eslint-disable-next-line no-unused-vars */
  81. login: ({ commit }, user) => {
  82. return new Promise((resolve, reject) => {
  83. auth.login(user)
  84. .then(() => {
  85. return resolve({
  86. status: "success",
  87. message: "Logged in!"
  88. });
  89. })
  90. .catch(err => {
  91. return reject({
  92. status: "error",
  93. message: err.message
  94. });
  95. });
  96. });
  97. },
  98. getUsernameFromId: ({ commit, state }, userId) => {
  99. return new Promise(resolve => {
  100. if (typeof state.userIdMap[`Z${userId}`] !== "string") {
  101. if (state.userIdRequested[`Z${userId}`] !== true) {
  102. commit("requestingUserId", userId);
  103. io.getSocket(socket => {
  104. socket.emit(
  105. "users.getUsernameFromId",
  106. userId,
  107. res => {
  108. if (res.status === "success") {
  109. commit("mapUserId", {
  110. userId,
  111. username: res.data
  112. });
  113. state.waitingForUserIdCallbacks[
  114. `Z${userId}`
  115. ].forEach(callback => {
  116. callback(res.data);
  117. });
  118. commit(
  119. "clearWaitingCallbacks",
  120. userId
  121. );
  122. return resolve(res.data);
  123. } else return resolve();
  124. }
  125. );
  126. });
  127. } else {
  128. commit("waitForUsername", {
  129. userId,
  130. callback: username => {
  131. return resolve(username);
  132. }
  133. });
  134. }
  135. }
  136. });
  137. }
  138. },
  139. mutations: {
  140. mapUserId(state, data) {
  141. state.userIdMap[`Z${data.userId}`] = data.username;
  142. state.userIdRequested[`Z${data.userId}`] = false;
  143. },
  144. requestingUserId(state, userId) {
  145. state.userIdRequested[`Z${userId}`] = true;
  146. if (!state.waitingForUserIdCallbacks[`Z${userId}`])
  147. state.waitingForUserIdCallbacks[`Z${userId}`] = [];
  148. },
  149. waitForUsername(state, data) {
  150. state.waitingForUserIdCallbacks[`Z${data.userId}`].push(
  151. data.callback
  152. );
  153. },
  154. clearWaitingCallbacks(state, userId) {
  155. state.waitingForUserIdCallbacks[`Z${userId}`] = [];
  156. }
  157. }
  158. },
  159. playlists: {
  160. namespaced: true,
  161. state: {
  162. editing: ""
  163. },
  164. getters: {},
  165. actions: {
  166. editPlaylist: ({ commit }, id) => commit("editPlaylist", id)
  167. },
  168. mutations: {
  169. editPlaylist(state, id) {
  170. state.editing = id;
  171. }
  172. }
  173. }
  174. };
  175. export default {
  176. namespaced: true,
  177. state,
  178. getters,
  179. actions,
  180. mutations,
  181. modules
  182. };