user.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* eslint no-param-reassign: 0 */
  2. /* eslint-disable import/no-cycle */
  3. import validation from "@/validation";
  4. import ws from "@/ws";
  5. import auth from "@/api/auth";
  6. const state = {};
  7. const getters = {};
  8. const actions = {};
  9. const mutations = {};
  10. const modules = {
  11. auth: {
  12. namespaced: true,
  13. state: {
  14. userIdMap: {},
  15. userIdRequested: {},
  16. pendingUserIdCallbacks: {},
  17. loggedIn: false,
  18. role: "",
  19. username: "",
  20. userId: "",
  21. banned: false,
  22. ban: {},
  23. gotData: false
  24. },
  25. actions: {
  26. /* eslint-disable-next-line no-unused-vars */
  27. register: ({ commit }, user) => {
  28. return new Promise((resolve, reject) => {
  29. const { username, email, password } = user;
  30. if (!email || !username || !password)
  31. return reject(new Error("Please fill in all fields"));
  32. if (!validation.isLength(email, 3, 254))
  33. return reject(
  34. new Error(
  35. "Email must have between 3 and 254 characters."
  36. )
  37. );
  38. if (
  39. email.indexOf("@") !== email.lastIndexOf("@") ||
  40. !validation.regex.emailSimple.test(email)
  41. )
  42. return reject(new Error("Invalid email format."));
  43. if (!validation.isLength(username, 2, 32))
  44. return reject(
  45. new Error(
  46. "Username must have between 2 and 32 characters."
  47. )
  48. );
  49. if (!validation.regex.azAZ09_.test(username))
  50. return reject(
  51. new Error(
  52. "Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _."
  53. )
  54. );
  55. if (!validation.isLength(password, 6, 200))
  56. return reject(
  57. new Error(
  58. "Password must have between 6 and 200 characters."
  59. )
  60. );
  61. if (!validation.regex.password.test(password))
  62. return reject(
  63. new Error(
  64. "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character."
  65. )
  66. );
  67. return auth
  68. .register(user)
  69. .then(res => {
  70. return resolve(res);
  71. })
  72. .catch(err => {
  73. return reject(new Error(err.message));
  74. });
  75. });
  76. },
  77. /* eslint-disable-next-line no-unused-vars */
  78. login: ({ commit }, user) => {
  79. return new Promise((resolve, reject) => {
  80. auth.login(user)
  81. .then(() => {
  82. return resolve({
  83. status: "success",
  84. message: "Logged in!"
  85. });
  86. })
  87. .catch(err => {
  88. return reject(new Error(err.message));
  89. });
  90. });
  91. },
  92. logout: () => {
  93. return new Promise((resolve, reject) => {
  94. return auth
  95. .logout()
  96. .then(() => {
  97. return resolve();
  98. })
  99. .catch(() => {
  100. return reject();
  101. });
  102. });
  103. },
  104. getUsernameFromId: ({ commit, state }, userId) => {
  105. return new Promise(resolve => {
  106. if (typeof state.userIdMap[`Z${userId}`] !== "string") {
  107. if (state.userIdRequested[`Z${userId}`] !== true) {
  108. commit("requestingUserId", userId);
  109. ws.socket.dispatch(
  110. "users.getUsernameFromId",
  111. userId,
  112. res => {
  113. if (res.status === "success") {
  114. commit("mapUserId", {
  115. userId,
  116. username: res.data
  117. });
  118. state.pendingUserIdCallbacks[
  119. `Z${userId}`
  120. ].forEach(cb => cb(res.data));
  121. commit("clearPendingCallbacks", userId);
  122. return resolve(res.data);
  123. }
  124. return resolve();
  125. }
  126. );
  127. } else {
  128. commit("pendingUsername", {
  129. userId,
  130. callback: username => {
  131. return resolve(username);
  132. }
  133. });
  134. }
  135. } else {
  136. resolve(state.userIdMap[`Z${userId}`]);
  137. }
  138. });
  139. },
  140. authData: ({ commit }, data) => {
  141. commit("authData", data);
  142. },
  143. banUser: ({ commit }, ban) => {
  144. commit("banUser", ban);
  145. },
  146. updateUsername: ({ commit }, username) => {
  147. commit("updateUsername", username);
  148. }
  149. },
  150. mutations: {
  151. mapUserId(state, data) {
  152. state.userIdMap[`Z${data.userId}`] = data.username;
  153. state.userIdRequested[`Z${data.userId}`] = false;
  154. },
  155. requestingUserId(state, userId) {
  156. state.userIdRequested[`Z${userId}`] = true;
  157. if (!state.pendingUserIdCallbacks[`Z${userId}`])
  158. state.pendingUserIdCallbacks[`Z${userId}`] = [];
  159. },
  160. pendingUsername(state, data) {
  161. state.pendingUserIdCallbacks[`Z${data.userId}`].push(
  162. data.callback
  163. );
  164. },
  165. clearPendingCallbacks(state, userId) {
  166. state.pendingUserIdCallbacks[`Z${userId}`] = [];
  167. },
  168. authData(state, data) {
  169. state.loggedIn = data.loggedIn;
  170. state.role = data.role;
  171. state.username = data.username;
  172. state.userId = data.userId;
  173. state.gotData = true;
  174. },
  175. banUser(state, ban) {
  176. state.banned = true;
  177. state.ban = ban;
  178. },
  179. updateUsername(state, username) {
  180. state.username = username;
  181. }
  182. }
  183. },
  184. playlists: {
  185. namespaced: true,
  186. state: {
  187. editing: "",
  188. playlists: []
  189. },
  190. actions: {
  191. editPlaylist: ({ commit }, id) => commit("editPlaylist", id),
  192. setPlaylists: ({ commit }, playlists) =>
  193. commit("setPlaylists", playlists)
  194. },
  195. mutations: {
  196. editPlaylist(state, id) {
  197. state.editing = id;
  198. },
  199. setPlaylists(state, playlists) {
  200. state.playlists = playlists;
  201. }
  202. }
  203. },
  204. preferences: {
  205. namespaced: true,
  206. state: {
  207. nightmode: false,
  208. autoSkipDisliked: true,
  209. activityLogPublic: false,
  210. activityWatch: false
  211. },
  212. actions: {
  213. changeNightmode: ({ commit }, nightmode) => {
  214. commit("changeNightmode", nightmode);
  215. },
  216. changeAutoSkipDisliked: ({ commit }, autoSkipDisliked) => {
  217. commit("changeAutoSkipDisliked", autoSkipDisliked);
  218. },
  219. changeActivityLogPublic: ({ commit }, activityLogPublic) => {
  220. commit("changeActivityLogPublic", activityLogPublic);
  221. },
  222. changeActivityWatch: ({ commit }, activityWatch) => {
  223. commit("changeActivityWatch", activityWatch);
  224. }
  225. },
  226. mutations: {
  227. changeNightmode(state, nightmode) {
  228. state.nightmode = nightmode;
  229. },
  230. changeAutoSkipDisliked(state, autoSkipDisliked) {
  231. state.autoSkipDisliked = autoSkipDisliked;
  232. },
  233. changeActivityLogPublic(state, activityLogPublic) {
  234. state.activityLogPublic = activityLogPublic;
  235. },
  236. changeActivityWatch(state, activityWatch) {
  237. state.activityWatch = activityWatch;
  238. }
  239. }
  240. }
  241. };
  242. export default {
  243. namespaced: true,
  244. state,
  245. getters,
  246. actions,
  247. mutations,
  248. modules
  249. };