I'm working on a system where user has multiple role-permissions. Suppose I've 3 users, User1 customer, User2 employee, User3 admin.
I've 3 sperate dashboard page for these 3 types of users. Based on user role I want to redirect the users to their dashboard page. I also want to peoject the route with guard so that if someone without permission wants to goto the page by manually typing the url, it will redirect to an 404 page.
Now the questions are :
What is the best way to store the user's role-permissions? Browser's local storage or vueX?
Shall I check the user's role-permissions in router.js in order to redirect them to their individual dashboard? Or this check should be in login.vue component?
Where to set the guards to check if user has permission to access the route and redirect to 404 page if unauthorised?
Please expain the answers in details if possible. I've already studied vue-router and routing in nuxt.js. If there is any other resources to get a clear picture about this issue please share in comment so that I can understand the process clearly.
import { constant } from 'lodash';
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
export const store = new Vuex.Store({
//Vuex States
state:{
permissions:[],
},
//Getters
getters:{
authPermissions: state => state.permissions
},
//mutation
mutations:{
setPermissions: (state, fetchedData) => (state.permissions = fetchedData)
},
//mutation with actions
actions:{
async fetchAuthPermissions({ commit }){
let token = localStorage.getItem('token');
let formData = new FormData();
formData.append("token", token);
const response = await axios.post("api/userPermissions/", formData);
commit('setPermissions', response.data.permissions);
}
}
})