user.js 6.6 KB

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