user.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import auth from "../../api/auth.js";
  2. import validation from "../../validation.js";
  3. const state = {};
  4. const getters = {};
  5. const actions = {};
  6. const mutations = {};
  7. const modules = {
  8. auth: {
  9. namespaced: true,
  10. state: {},
  11. getters: {},
  12. actions: {
  13. /* eslint-disable-next-line no-unused-vars */
  14. register: ({ commit }, user, recaptchaId) => {
  15. return new Promise((resolve, reject) => {
  16. const { username, email, password } = user;
  17. if (!email || !username || !password)
  18. return reject({
  19. status: "error",
  20. message: "Please fill in all fields"
  21. });
  22. if (!validation.isLength(email, 3, 254))
  23. return reject({
  24. status: "error",
  25. message:
  26. "Email must have between 3 and 254 characters."
  27. });
  28. if (
  29. email.indexOf("@") !== email.lastIndexOf("@") ||
  30. !validation.regex.emailSimple.test(email)
  31. )
  32. return reject({
  33. status: "error",
  34. message: "Invalid email format."
  35. });
  36. if (!validation.isLength(username, 2, 32))
  37. return reject({
  38. status: "error",
  39. message:
  40. "Username must have between 2 and 32 characters."
  41. });
  42. if (!validation.regex.azAZ09_.test(username))
  43. return reject({
  44. status: "error",
  45. message:
  46. "Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _."
  47. });
  48. if (!validation.isLength(password, 6, 200))
  49. return reject({
  50. status: "error",
  51. message:
  52. "Password must have between 6 and 200 characters."
  53. });
  54. if (!validation.regex.password.test(password))
  55. return reject({
  56. status: "error",
  57. message:
  58. "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character."
  59. });
  60. auth.register(user, recaptchaId)
  61. .then(() => {
  62. return resolve({
  63. status: "success",
  64. message: "Account registered!"
  65. });
  66. })
  67. .catch(err => {
  68. return reject({
  69. status: "error",
  70. message: err.message
  71. });
  72. });
  73. });
  74. },
  75. /* eslint-disable-next-line no-unused-vars */
  76. login: ({ commit }, user) => {
  77. return new Promise((resolve, reject) => {
  78. auth.login(user)
  79. .then(() => {
  80. return resolve({
  81. status: "success",
  82. message: "Logged in!"
  83. });
  84. })
  85. .catch(err => {
  86. return reject({
  87. status: "error",
  88. message: err.message
  89. });
  90. });
  91. });
  92. }
  93. },
  94. mutations: {}
  95. },
  96. playlists: {
  97. namespaced: true,
  98. state: {
  99. editing: ""
  100. },
  101. getters: {},
  102. actions: {
  103. editPlaylist: ({ commit }, id) => commit("editPlaylist", id)
  104. },
  105. mutations: {
  106. editPlaylist(state, id) {
  107. state.editing = id;
  108. }
  109. }
  110. }
  111. };
  112. export default {
  113. namespaced: true,
  114. state,
  115. getters,
  116. actions,
  117. mutations,
  118. modules
  119. };