user.js 3.6 KB

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