123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- import auth from "../../api/auth.js";
- import io from "../../io.js";
- import validation from "../../validation.js";
- const state = {};
- const getters = {};
- const actions = {};
- const mutations = {};
- const modules = {
- auth: {
- namespaced: true,
- state: {
- userIdMap: {},
- userIdRequested: {},
- waitingForUserIdCallbacks: {}
- },
- getters: {},
- actions: {
- /* eslint-disable-next-line no-unused-vars */
- register: ({ commit }, user) => {
- return new Promise((resolve, reject) => {
- const { username, email, password } = user;
- if (!email || !username || !password)
- return reject({
- status: "error",
- message: "Please fill in all fields"
- });
- if (!validation.isLength(email, 3, 254))
- return reject({
- status: "error",
- message:
- "Email must have between 3 and 254 characters."
- });
- if (
- email.indexOf("@") !== email.lastIndexOf("@") ||
- !validation.regex.emailSimple.test(email)
- )
- return reject({
- status: "error",
- message: "Invalid email format."
- });
- if (!validation.isLength(username, 2, 32))
- return reject({
- status: "error",
- message:
- "Username must have between 2 and 32 characters."
- });
- if (!validation.regex.azAZ09_.test(username))
- return reject({
- status: "error",
- message:
- "Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _."
- });
- if (!validation.isLength(password, 6, 200))
- return reject({
- status: "error",
- message:
- "Password must have between 6 and 200 characters."
- });
- if (!validation.regex.password.test(password))
- return reject({
- status: "error",
- message:
- "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character."
- });
- auth.register(user)
- .then(() => {
- return resolve({
- status: "success",
- message: "Account registered!"
- });
- })
- .catch(err => {
- return reject({
- status: "error",
- message: err.message
- });
- });
- });
- },
- /* eslint-disable-next-line no-unused-vars */
- login: ({ commit }, user) => {
- return new Promise((resolve, reject) => {
- auth.login(user)
- .then(() => {
- return resolve({
- status: "success",
- message: "Logged in!"
- });
- })
- .catch(err => {
- return reject({
- status: "error",
- message: err.message
- });
- });
- });
- },
- getUsernameFromId: ({ commit, state }, userId) => {
- return new Promise(resolve => {
- if (typeof state.userIdMap[`Z${userId}`] !== "string") {
- if (state.userIdRequested[`Z${userId}`] !== true) {
- commit("requestingUserId", userId);
- io.getSocket(socket => {
- socket.emit(
- "users.getUsernameFromId",
- userId,
- res => {
- if (res.status === "success") {
- commit("mapUserId", {
- userId,
- username: res.data
- });
- state.waitingForUserIdCallbacks[
- `Z${userId}`
- ].forEach(callback => {
- callback(res.data);
- });
- commit(
- "clearWaitingCallbacks",
- userId
- );
- return resolve(res.data);
- } else return resolve();
- }
- );
- });
- } else {
- commit("waitForUsername", {
- userId,
- callback: username => {
- return resolve(username);
- }
- });
- }
- }
- });
- }
- },
- mutations: {
- mapUserId(state, data) {
- state.userIdMap[`Z${data.userId}`] = data.username;
- state.userIdRequested[`Z${data.userId}`] = false;
- },
- requestingUserId(state, userId) {
- state.userIdRequested[`Z${userId}`] = true;
- if (!state.waitingForUserIdCallbacks[`Z${userId}`])
- state.waitingForUserIdCallbacks[`Z${userId}`] = [];
- },
- waitForUsername(state, data) {
- state.waitingForUserIdCallbacks[`Z${data.userId}`].push(
- data.callback
- );
- },
- clearWaitingCallbacks(state, userId) {
- state.waitingForUserIdCallbacks[`Z${userId}`] = [];
- }
- }
- },
- playlists: {
- namespaced: true,
- state: {
- editing: ""
- },
- getters: {},
- actions: {
- editPlaylist: ({ commit }, id) => commit("editPlaylist", id)
- },
- mutations: {
- editPlaylist(state, id) {
- state.editing = id;
- }
- }
- }
- };
- export default {
- namespaced: true,
- state,
- getters,
- actions,
- mutations,
- modules
- };
|