user.js 4.3 KB

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