const namespaced = true; const state = { jwt: null, userData: null, userRole: null, }; const mutations = { SET_JWT(state, jwt) { state.jwt = jwt; }, SET_USER_DATA(state, userData) { state.userData = userData; }, SET_USER_ROLE(state, userRole) { state.userRole = userRole; }, LOG_OUT(state) { state.jwt = null; state.userData = null; state.userRole = null; }, }; const getters = { jwt: (state) => state.jwt, userData: (state) => state.userData, userRole: (state) => state.userRole, }; const actions = { setJwt({ commit }, jwt) { commit("SET_JWT", jwt); }, setUserData({ commit }, userData) { commit("SET_USER_DATA", userData); }, setUserRole({ commit }, userRole) { commit("SET_USER_ROLE", userRole); }, logOut({ commit }) { commit("LOG_OUT"); }, tryLogin(context) { const jwtToken = localStorage.getItem("jwt"); const userData = localStorage.getItem("userData"); const role = localStorage.getItem("roleName"); context.commit("SET_JWT", jwtToken); context.commit("SET_USER_DATA", userData); context.commit("SET_USER_ROLE", role); }, }; export default { namespaced, state, mutations, getters, actions, };