0

UPDATE

CodeSandbox

What i try to do:

i am working on a permissionerMixin which should handle a websites permission for authenticated users. for example if a logged in user has no permission to see a specific part of the website i handle the components with a v-if="allowedToSee". So far so good. i store the whole user permission object in my vuex store.

the data came from a rest api and looks like this:

const rights = [
  {
    name: 'findMe1',
    value: false,
  },
  {
    name: 'findMe2',
    value: false,
  },
  {
    name: 'findMe3',
    value: false,
  },
  {
    name: 'findMe4',
    value: false,
  }
]

now back to my mixin and and how i load the data from the api:

import axios from 'axios';
export const permissionerMixin = {
  methods: {
    async getRightsFromActiveUser() {
      axios.get(`/not/the/real/path/${this.$store.state.User.activeUser.id}`)
        .then((response) => {
          return this.$store.commit('addActiveUserRights', response.data);
        })
        .catch((error) => {
          console.log(error.response);
        });
    },
    async permissionFor(rightName) {
      const rights = await this.getRightsFromActiveUser();
      for (const right of rights) {
        if (right.name == rightName) {
          return right.value;
        }
      }
    }
  }
}

as u can see i have two functions which work together. getRightsFromActiveUser() is simply a getter for the right object i mentioned at the beginning. it takes the actual data and puts it in the vuex store with a mutation:

const state = {
  activeUser: {
    id: 0,
    permissions: {}
  }
};
const getters = {};
const actions = {};
const mutations = {
  addActiveUserId (state, id) {
    state.activeUser.id = id;
  },
  addActiveUserRights (state, rights) {
    state.activeUser.permissions = rights;
  }
};
export default {
  state,
  getters,
  actions,
  mutations,
};

right after this we have the actual init function permissionFor(rightName) which should do the magic and should give me a boolean return value to handle the permissionings.

the one big problem now is that i instead of getting a boolean return, i get a [object Promise], thats because i am stupid and i don't get that promise thing in my head.

at the end i simply want to add this function to a vue component with an

v-if="permissionFor('whatEver')" to solve the permission handling.

1
  • i have your code open and the first thing i see is mixing chained promises .then() with async/await. I would say pick one and stick to it. The second thing that messes with me is return this.$store.commit('addActiveUserRights', response.data); Your returning the actual commit. Should you be storing AND returning maybe? I'll put that in an answer for you to see and it may help. I have never tried to log the return from a commit to see what it gives you. Commented Jun 15, 2020 at 14:54

1 Answer 1

1

pulled the return into it's own statement following the commit. Not sure what your response object looks like from the back end but this look a cleaner to me personally and able to be read later. Check it out and see how things change, if at all.

import axios from 'axios';
export const permissionerMixin = {
  methods: {
    async getRightsFromActiveUser() {
      try { 
        let returnData = await axios.get(`/not/the/real/path/${this.$store.state.User.activeUser.id}`)
        this.$store.commit('addActiveUserRights', response.data);
        return returnData.data
      } catch (error) {
        console.log(error.response);
      }
    },
    async permissionFor(rightName) {
      try {
        const rights = await this.getRightsFromActiveUser();
        for (const right of rights) {
          if (right.name == rightName) {
            return right.value;
          }
        }
      } catch(error){
        console.log(error.response);
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

7 Comments

what did you get?
still the same [object Promise]
what do you get if you console.log the returnData.data object directly AFTER the axios call?
i get the requested data but the code also goes into the permissionFor() -> catch(error){console.log(error.response);}
i try to do a CodeSandbox
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.